This chapter is a brief introduction to shell scripting. It will assume the reader is mostly familiar with script basics and will serve as a refresher.
The topics covered in this chapter are as follows:
The general format of a script.
How to make a file executable.
Creating a good Usage message and handling return codes.
Show how to pass parameters from the command line.
Show how to validate parameters by using conditional statements.
Explain how to determine the attributes of files.
The book will assume that the user has put a (
.) at the beginning of the path for that account. If not, to run a script prepend
./ to the filename. For example:
The scripts will be made executable using the
It is suggested that the user create a directory under his guest account specifically for the examples in this book. For example, something like this works well:
Of course, feel free to use whatever works best for you.
Following the general format of a bash script the very first line will contain this and nothing else:
# This entire line is a comment
chmod 755 filename # This text after the # is a comment
Use comments however you deem appropriate. Some people comment every line, some don't comment anything. I try to strike a balance somewhere in the middle of those two extremes.
I have found that most people are comfortable using vi to create and edit text documents under a UNIX/Linux environment. This is fine as vi is a very dependable application. I would suggest not using any type of word processing program, even if it claims to have a code development option. These programs might still put invisible control characters in the file which will probably cause the script to fail. This can take hours or even days to figure out unless you are good at looking at binary files.
Also, in my opinion, if you plan to do a lot of script and/or code development I suggest looking at some other text editor other than vi. You will almost certainly become more productive.
#!/bin/sh # # 03/27/2017 # exit 0
By convention, in this book the script lines will usually be numbered. This is for teaching purposes only, in an actual script the lines are not numbered.
Here is the same script with the lines numbered:
1 #!/bin/sh 2 # 3 # 03/27/2017 4 # 5 exit 0 6
Line 1 tells the operating system which shell interpreter to use. Note that on some distributions
/bin/shis actually a symbolic link to the interpreter.
Lines that begin with a
#are comments. Also, anything after a
#is also treated as a comment.
It is good practice to include a date in your scripts, either here in the comments section and/or in the
Usagesection (covered next).
Line 5 is the return code from this script. This is optional but highly recommended.
Line 6 is a blank line and is the last line of the script.
Using your favorite text editor, edit a new file named
script1 and copy the preceding script without the line numbers into it. Save the file.
To make the file into an executable script run this:
$ chmod 755 script1
Now run the script:
If you did not prepend a
. to your path as mentioned in the introduction then run:
Now check the return code:
$ echo $? 0
Here is a script that does something a little more useful:
#!/bin/sh # # 3/26/2017 # ping -c 1 google.com # ping google.com just 1 time echo Return code: $?
ping command returns a zero on success and non-zero on failure. As you can see,
echoing $? shows the return value of the command preceding it. More on this later.
1 #!/bin/sh 2 # 3 # 6/13/2017 4 # 5 if [ $# -ne 1 ] ; then 6 echo "Usage: script3 file" 7 echo " Will determine if the file exists." 8 exit 255 9 fi 10 11 if [ -f $1 ] ; then 12 echo File $1 exists. 13 exit 0 14 else 15 echo File $1 does not exist. 16 exit 1 17 fi 18
5checks to see if a parameter was given. If not, lines
9are executed. Note that is it usually a good idea to include an informative
Usagestatement in your script. It is also good to provide a meaningful return code.
11checks to see if the file exists and if so lines
13are executed. Otherwise lines
A word about return codes: It is standard practice under Linux/UNIX to return zero if the command was successful, and non-zero if not. In this way the code returned can mean something useful, not only to humans, but to other scripts and programs as well. However, it is not mandatory to do this. If you want your script to return codes that are not errors but indicate some other condition by all means do so.
1 #!/bin/sh 2 # 3 # 6/13/2017 4 # 5 if [ $# -ne 1 ] ; then 6 echo "Usage: script4 filename" 7 echo " Will show various attributes of the file given." 8 exit 255 9 fi 10 11 echo -n "$1 " # Stay on the line 12 13 if [ ! -e $1 ] ; then 14 echo does not exist. 15 exit 1 # Leave script now 16 fi 17 18 if [ -f $1 ] ; then 19 echo is a file. 20 elif [ -d $1 ] ; then 21 echo is a directory. 22 fi 23 24 if [ -x $1 ] ; then 25 echo Is executable. 26 fi 27 28 if [ -r $1 ] ; then 29 echo Is readable. 30 else 31 echo Is not readable. 32 fi 33 34 if [ -w $1 ] ; then 35 echo Is writable. 36 fi 37 38 if [ -s $1 ] ; then 39 echo Is not empty. 40 else 41 echo Is empty. 42 fi 43 44 exit 0 # No error 45
9: If the script is not run with a parameter display the
Usagemessage and exit with a return code of
11shows how to
echoa string of text but still stay on the line (no linefeed).
13shows how to determine if the parameter given is an existing file.
15leaves the script as there is no reason to continue if the file doesn't exist.
The meaning of the remaining lines can be determined by the script itself. Note that there are many other checks that can be performed on a file, these are just a few.
Here are some examples of running
script4 on my system:
guest1 $ script4 Usage: script4 filename Will show various attributes of the file given. guest1 $ script4 /tmp /tmp is a directory. Is executable. Is readable. Is writable. Is not empty. guest1 $ script4 script4.numbered script4.numbered is a file. Is readable. Is not empty. guest1 $ script4 /usr /usr is a directory. Is executable. Is readable. Is not empty. guest1 $ script4 empty1 empty1 is a file. Is readable. Is writable. Is empty. guest1 $ script4 empty-noread empty-noread is a file. Is not readable. Is empty.
#!/bin/sh # # 3/27/2017 # echo The number of parameters is: $# exit 0
Let's try a few examples:
guest1 $ script5 The number of parameters is: 0 guest1 $ script5 parm1 The number of parameters is: 1 guest1 $ script5 parm1 Hello The number of parameters is: 2 guest1 $ script5 parm1 Hello 15 The number of parameters is: 3 guest1 $ script5 parm1 Hello 15 "A string" The number of parameters is: 4 guest1 $ script5 parm1 Hello 15 "A string" lastone The number of parameters is: 5
Remember that a quoted string is counted as 1 parameter. This is a way to pass strings that contain blank characters.
#!/bin/sh # # 3/27/2017 # if [ $# -ne 3 ] ; then echo "Usage: script6 parm1 parm2 parm3" echo " Please enter 3 parameters." exit 255 fi echo Parameter 1: $1 echo Parameter 2: $2 echo Parameter 3: $3 exit 0
The lines of this script were not numbered as it is rather simple. The
$# contains the number of parameters that were passed to the script.
In this chapter we looked at the basics of script design. How to make a script executable was shown as was creating an informative
Usage message. The importance of return codes was also covered as was the use and validation of parameters.
The next chapter will go into more detail about variables and conditional statements.