How to Create/Write a Simple/Sample Linux Shell/Bash Script
by matt392 in Circuits > Linux
81029 Views, 20 Favorites, 0 Comments
How to Create/Write a Simple/Sample Linux Shell/Bash Script
This Instructable will show you how to create and execute a simple Linux shell script.
A shell script, at its simplest, is an automated series of Linux commands stored for repeated later use. For this example, we are going to use a number of simple commands, like ls, pwd, chmod, touch, uname, ping and grep.
Choose Text Editor
Shell scripts are written using text editors. On Linux systems, there are a number to choose from: Vim, Emacs, Nano, Pico, Kedit, Gedit, Geany, Notepad++, Kate, Jed or LeafPad.
Once you have chosen a text editor, start the text editor, open a new file to begin to typing a shell script.
Type in Commands and Echo Statements
Start to type in basic commands that you would like the script to run.
Be sure to type each command on a separate line.
For example, to print out words to the screen use the "echo" command:
echo "This statement will print out to the screen."
To list files in a directory, type:
echo "Now we are going to list files."
ls
To print the current directory you are in, type:
echo "Next we are going to print the directory we are in:"
pwd
Save the file under the name: FirstShellScript.sh
Make File Executable
Now that the file has been saved, it needs to be made executable. This is done using the chmod command. On your Linux command line type:
chmod 555 FirstShellScript.sh
This will allow you to execute the shell script to run the commands contained within it.
Run the Shell Script
1. To run the shell script, navigate to the directory where the file you just saved exists.
2. Now type the following [be sure to type the "dot slash" before it!]:
./FirstShellScript.sh
3. Then hit the Enter key to execute it
4. The commands that you saved in the shell script will now run.
Longer Shell Script
If you would like to try out a longer shell script, please copy the shell script below (and attached as PDF, ODT, Bash and text files) into your text editor, save it,make it executable and run it. As you learn more Linux commands, you can create more complex shell scripts.
=========================
echo "Matt's test shell script"
echo "Created using the vim editor"
echo "Using a shell running in the browser"
echo "from the website www.PythonAnywhere.com"
echo "=========="
echo "List some files:" ls
echo "List file in the long format:" ls -l
echo "=========="
echo "Now we are going to change the permissions on this shell script:"
echo "Read: 4, Write: 2, Execute: 1"
chmod 755 mattshell.sh
echo "=========="
echo "Creating a file using the touch command"
touch testfile.text
echo "Please note that the file extensions in Linux could be anything."
echo "Now we are going to list the file we just created:"
ls testfile.text -l
echo "=========="
echo "Next we are going to pipe the ls command into more:"
ls|more
echo "=========="
echo "Now we are going to run the ls command and write it to a text file"
ls > listoffiles.text
echo "Next we are going to use the more command to view the file we just created"
more listoffiles.text
echo "We are going to find out what kernel we are running:"
echo "Kernel release:"; uname -r
echo "========"
echo "Next we are going to find the answer to the question of who we are:"
whoami
echo "========="
echo "Let's send a packet across the US to LA:"
ping -c 1 www.ucla.edu
echo "Our hostname is:" hostname
echo "Here we are going to change the listoffiles.text to inital caps:"
cp listoffiles.text ListOfFiles.text
ls -l ListOfFiles*
echo "========"
echo "Now we are going to find out where we are:"
pwd
echo "Create a file named after Shakespeare quote:"
touch tobeornottobe.text
ls -l tobe*
echo "========"
echo "Just a quick use of the grep command using wildcard"
echo "We will pipe the contents of ls into grep:"
ls | grep tobe*