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.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: 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.
Step 2: 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."
To print the current directory you are in, type:
echo "Next we are going to print the directory we are in:"
Save the file under the name: FirstShellScript.sh
Step 3: 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.
Step 4: 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!]:
3. Then hit the Enter key to execute it
4. The commands that you saved in the shell script will now run.
Step 5: Longer Shell Script
If you would like to try out a longer shell script, please copy the shell script below (and attached as PDF and ODT 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 "List some files:" ls
echo "List file in the long format:" ls -l
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 "Creating a file using the touch command"
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 "Next we are going to pipe the ls command into more:"
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"
echo "We are going to find out what kernel we are running:"
echo "Kernel release:"; uname -r
echo "Next we are going to find the answer to the question of who we are:"
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 "Now we are going to find out where we are:"
echo "Create a file named after Shakespeare quote:"
ls -l tobe*
echo "Just a quick use of the grep command using wildcard"
echo "We will pipe the contents of ls into grep:"
ls | grep tobe*