Beginners Guide to Writing Shell Scripts

Creating Shell Scripts

A shell script is a text file that contains a sequence of UNIX commands. The shell script is often a command sequence for which you have a repeated use. Typically, you execute the sequence of commands in the shell script by entering the name of the shell script on the command line. To create a shell script, create a file, and put UNIX commands into it. UNIX commands include standard utilities, user programs, and the names of other scripts that you need to accomplish your task.

A script file can have any name following the conventions of regular file names in the operating-system environment. When naming your shell programs you should avoid using names that conflict with existing UNIX commands or shell functions. The following example script uses the echo command to inform the user about the output to expect before the actual command that creates the output is called. It is good practice when writing scripts to inform users about what is happening so they have an idea of whether the script is running correctly.

$ vi firstscript.sh
#!/bin/sh
clear
echo "SCRIPT BEGINS"
echo "Hello $LOGNAME!"
echo
echo "Todays date and time: \c"
date
echo
mynum=21
myday="Monday"
echo "The value of mynum is $mynum"
echo "The value of myday is $myday"
echo
echo "SCRIPT FINISHED!!"
echo

Executing a Shell Script

To execute a shell script as you do other commands on the system, first assign it the execute permission with the chmod command; for example:

$ chmod 744 scriptname
$ scriptname

This is the preferred and most common method for executing a shell script. A subshell is created to execute the specified script. Give execute permission only to users who need to execute the script. When a script is executed in a subshell, the variables, aliases, and functions created and used in the script are known only in the subshell. After the script finishes and control returns to the parent shell, the variables, functions, and other changes to the state of the shell made by the script are no longer known.

Generally, you execute a script using the shell command only to invoke certain options on a script that you do not have permission to execute (or do not want to execute).

$ ksh scriptname
$ sh scriptname

Again, a subshell is created to execute the specified script. This is used in situations when you want the shell to start with specific options not explicitly specified in the script. The typical situation is when you want to turn on and off debugging for the entire script. The following example shows executing a script with debugging:

$ ksh -x scriptname $ sh -x scriptname

The following example runs the script in the current shell:

$ . ./scriptname

To execute the shell script in the current shell, you do not need to execute permissions on the script. You are running the commands in the current shell, so any operations you perform on shell variables or aliases take effect in the current shell and are seen after the shell script terminates.

Executing the firstscript.sh Script

The following is the firstscript.sh script.

$ cat firstscript.sh
#!/bin/sh
clear
echo "SCRIPT BEGINS"
echo "Hello $LOGNAME!"
echo
echo "Todays date and time: \c"
date
echo
mynum=21
myday="Monday"
echo "The value of mynum is $mynum"
echo "The value of myday is $myday"
echo
echo "SCRIPT FINISHED!!"
echo

Variables created by the script are not known to the shell that executed it. The script is executed in a subshell, and changes made there do not affect the parent shell.

$ chmod 744 firstscript.sh

$ ./firstscript.sh
SCRIPT BEGINS
Hello root!

Todays date and time: Tue May  5 11:38:13 MDT 2009

The value of mynum is 21
The value of myday is Monday

SCRIPT FINISHED!!
$ echo $mynum

$ echo $myday
$ sh firstscript.sh
SCRIPT BEGINS
Hello root!
Todays date and time: Tue May  5 11:38:57 MDT 2009
The value of mynum is 21
The value of myday is Monday
SCRIPT FINISHED!!
$ echo $mynum

$ echo $myday

Starting a Script With the #! Characters

When running a script that has execute permission by typing the script name on the command line, you should know the subshell (sh, csh, ksh, bash, or other subshell) that will run the script. The first line of the script determines the shell that is created as the interpreter of the script. The first two characters on the first line in a script are #!. The current shell interprets what follows as the path name for the subshell to execute the script

NOTE:“The first two characters on the first line” means there must be no blank lines or spaces before these characters.

In the following examples, the specified shell is forked as the subshell to run the script.

#!/bin/sh
#!/bin/csh
#!/bin/ksh

Although the syntax #! path_for_shell allows the script writer to specify exactly in what shell the script should be run, not all operating systems recognize this syntax. Therefore, scripts written in this way might not run in exactly the same way on all operating systems.

Base the shell you choose on two rules:

  1. For a system boot script, use the Bourne shell (/sbin/sh).
  2. For a typical script, use the shell that you are most comfortable with or the one that supports all the features you need. Hence, if you want to use the alias feature or true integer variables, use the Korn shell. If you know the C language, the C shell might be the best shell for you to use

Putting Comments in a Script

The persons who write scripts usually are not the only persons who read them. Many persons run scripts written by others. If they view the contents of the script file to see what the script does, it is helpful if the author of the script included some explanations in comments. It is a good practice to put comments into programs and shell scripts. The comments should explain the purpose of the script and should explain any specific lines that might be especially confusing.

The example script, scriptwithcomments.sh, has a comment at the beginning of the script explaining what the script does when it runs. The line with the date statement contains a comment explaining the output produced by the date statement.

$ cat scriptwithcomments.sh
#!/bin/sh
# This script clears the window, greets the user,
# and displays the current date and time.
clear                                   # Clear the window
echo "SCRIPT BEGINS"
echo "Hello $LOGNAME!" # Greet the user echo
echo "Todays date and time: \c" date                                    # Display current date and time echo
mynum=21                                # Set a local shell variable
myday="Monday"                          # Set a local shell variable

echo "The value of mynum is $mynum"
echo "The value of myday is $myday"
echo
echo "SCRIPT FINISHED!!"
echo
$ ./scriptwithcomments.sh
SCRIPT BEGINS
Hello user1!

Todays date and time: Tue Nov 24 15:01:22 IST 2009

The value of mynum is 21
The value of myday is Monday

SCRIPT FINISHED!!

The addition of comments does not affect the execution of the script unless a syntactical error is introduced when the comments are added. The comments are there for documentation purposes, so someone reading the script will have an idea of what will occur when the script is executed. Comments are sometimes the first lines entered in a shell script. The comments can list the step-by-step operations that need to take place. Follow each comment line by the operating system commands to carry out the operation.