Here is an example of a very simple script. It might not look like much but this is the basis for every script:
#!/bin/sh # # 03/27/2017 # exit 0
Here is the same script with the lines numbered:
1 #!/bin/sh 2 # 3 # 03/27/2017 4 # 5 exit 0 6
Here is an explanation for each line:
/bin/sh
is actually a symbolic link to the interpreter.#
are comments. Also, anything after a #
is also treated as a comment.Usage
section (covered next).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:
$ script1
If you did not prepend a .
to your path as mentioned in the introduction then run:
$ ./script1
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: $?
The 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
Here is an explanation for each line:
5
checks to see if a parameter was given. If not, lines 6
through 9
are executed. Note that is it usually a good idea to include an informative Usage
statement in your script. It is also good to provide a meaningful return code.11
checks to see if the file exists and if so lines 12
-13
are executed. Otherwise lines 14
-17
are run.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
Here is an explanation for each line:
5
-9
: If the script is not run with a parameter display the Usage
message and exit with a return code of 255
.11
shows how to echo
a string of text but still stay on the line (no linefeed).13
shows how to determine if the parameter given is an existing file.15
leaves 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.
This next script shows how to determine the number of parameters that were passed to it:
#!/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
This next script shows how to handle multiple parameters in more detail:
#!/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.