16.5.5 Changing Your Environment from the Command Line

16.6 Creating and Running a Shell Script

Shell scripts provide an easy way to execute tedious commands, large or complicated sequences of commands, and routine tasks. A shell script is a file that contains one or more commands. When you type the name of a shell script file, the system executes the command sequence contained by the file.

16.6.1 General Script Remarks

You can create a shell script using a text editor. Your script can contain both operating system commands and shell built-in commands.

The following steps are general guidelines for writing shell scripts:

  1. Use a text editor to create and save a file. You can include any combination of shell and operating system commands in the shell script file.

    By convention, shell scripts that are not set up for use by many users are stored in the $HOME/bin directory.

    Note

    The operating system does not support the setuid or setgid subroutines within a shell script.

  2. Use the chmod command to allow only the owner to run (or execute) the file. For example, if your file is named myscript, enter:
    chmod u=rwx myscript
    

  3. Enter the script name on the command line to run the shell script. To run the shell script, myscript, enter:
    myscript
    

    Note

    You can run a shell script without making it executable if a shell command (ksh, bsh, or csh) precedes the shell script file name on the command line. For example, to run a nonexecutable file named myscript under the control of the Korn shell, type:

    # ksh myscript
    

    When you run an executable shell script in either the Korn (the POSIX Shell) or Bourne shell, the commands in the script are carried out under the control of the current shell (the shell from which the script is started) unless you specify a different shell. When you run an executable shell script in the C shell, the commands in the script are carried out under the control of the Bourne shell (/usr/bin/bsh) unless you specify a different shell.

    You can cause a shell script to run in a specific shell by including the shell within the shell script. To run an executable shell script under a specific shell, enter #!Path on the first line of the shell script. The #! characters identify the file type. The Path variable specifies the path name of the shell from which to run the shell script. For example, if the first line in a shell script is #!/usr/bin/bsh, the script is run under control of the Bourne shell.

    When you precede a shell script file name with a shell command, the shell specified on the command line overrides any shell specified within the script file itself. Therefore, entering ksh myfile runs the file named myfile under the control of the Korn shell, even if the first line of myfile is #!/usr/bin/csh.

16.6.2 Creating a Script

In this section, the Korn shell (ksh) will be the focus to create a script that will run a backup. The script will start with daily backups then move through to weekly and then finally a monthly backup. It will also need user input to continue or abort the operation.

The shell script should have the following format:

16.6.2.1 The #!/usr/bin/ksh

The first line of the Korn shell script should begin with:

#!/usr/bin/ksh

in order for the shell to execute under the control of the Korn shell, irrespective of the shell that called it.

16.6.2.2 Script Comments

Script comments is a description of the script that begin with the pound (#) sign. It will have all comments about the script or explain actions of the script.

16.6.2.3 Search Path

The shell script should include a search path. A search path is used because if a user with a different search path tries to use the script, it may fail.

16.6.2.4 Shell Variables

Variables are used to hold information that will be passed onto the script for utilization.

In the backup example the variables will be used to give the day of the week and day of the month variables to the rest of the script to determine the type of backup to use. The bye variable is also used for the exit routine.

16.6.2.5 Standard I/O and Redirection in Scripts

There are three types of I/O in scripts namely stdin, stdout, and stderr. Standard input (stdin) normally comes from the keyboard. Standard Output (stdout) normally goes to the screen. Standard Error (stderr) normally goes to the screen. Each of these errors has a descriptor, for stdin it is 0, for stdout it is 1 and for stderr it is 2. You can redirect these to or from a file if needed as described below:

<
redirect input
>
redirect output
>>
append output
2>
redirect stderr
1>&2
redirect stdout to stderr
2>&1
redirect stderr to stdout

16.6.2.6 Exit Status

A good shell script should have a single point of exit. The exit variable can be selected by a variable and used during the script.

16.6.2.7 The if/then statement

The if/then statement is the most basic conditional statement. What the if/then statement does is read a variable, and if the variable is true it executes a command. It executes between if and fi.

if/then
if [ $variable = true ]
then
command(s)
fi

16.6.2.8 The if/else Statement

The if/else statement is the next logical step to the if/then statement. What the if/else statement does is read the variable, if the variable is true then it executes a command else, which it executes another command. It executes between if and fi.

if/else
if [ $variable = true ]
then
command(s)
else
command(s)
        fi

16.6.2.9 The if/elif/else statement

The if/elif/else statement is again a progression on the if/else statement. What it does is read the variable if it is true then it executes a command, if another statement is true (elif) it performs that command until it reaches the else command, which tells the script that if none of the above are true execute the else command. It executes between if and fi.

if/elif/else Statement Format
if [ $variable = true ]
then
command(s)
elif [ $variable = true ]
then
command(s)
else
command(s)
fi

16.6.2.10 The while loop

The while loop basically tells the script that while a statement is true it will execute the loop until the statement becomes false. It executes the commands between do and done.

while
while [ $statement = true ]
do
command(s)
done

16.6.2.11 The for loop

The for loop is used to execute commands a set number of times. The number of times is determined by the list after the for command. It executes the commands between do and done.

for
for wordlist variables
do
command(s)
done

16.6.2.12 The case Command

The case command is used as a substitute for the if/elif commands. What the case command does is read a variable, and then according to the response it will execute a command. It executes the commands between case and esac.

case
case variable in
value 1)
command(s) ;;
value 2)
command(s) ;;
esac

Now that you have a basic idea of what is needed in a script, here is an example of a script called mybackup.

First line of script
#!/usr/bin/ksh
Script Comments
# Script Name: mybackup
# Author: I.T. Wasme
# This script will run a backup.
Script Variables
# Set variables here
DAILY=`date +%a`
MONTHLY=`date +%d`
BYE=stay
Script Start
# Start Script Here
Start of the while loop
while [ $BYE  = stay ]
do
Start of the for loop
for i in rootvg uservg tempvg
do
  # Interactive Prompting.
Standard I/O. First if/then statement
  if tty -s
  then
    echo "\n\n\n\n\n\n\n\n\n\nWould you like you like to backup" $i
    echo "\n Type Y/y to CONTINUE or N/n to ABORT: \c"; read inp
Start of the case command. Reading stdin
        case "$inp" in
                y*|Y*)
                echo  Backup will Run
                BYE=stay
                ;;
                n*|N*|*)
                echo    Backup will Not Run
Exit status
                BYE=go
End of case command
        esac
End first if statement
   fi
Start first if/else statement
        if [ $BYE = stay ]
        then
                # Backup Section Starts here
                echo "\nBackup procedure starting"
Start first if/elif/else statement
                        if [ $MONTHLY = 25  ]
                        then
                                echo This will run the MONTHLY BACKUP
savevg -f/dev/rmt0 $i
                        elif [ $DAILY = Fri ]
                        then
echo Will run a WEEKLY BACKUP
savevg -mf/dev/rmt0 $i
                        else
echo Will run a DAILY BACKUP
savevg -ef/dev/rmt0 $i
End first if/elif/else statement
                        fi
        else
Exit status
        BYE=go
End first if/else statement
        fi
End for loop
   done
Set exit point for multiple exit status points in script
    # Set single exit point
Start second if statement
    if [ $BYE = go ]
    then
        echo Backup procedure complete
Actual exit point
        exit
End second if statement
    fi
End while loop and shell exits
done

The preceding example is just a basic idea of what is needed to create a basic script and how you can utilize the various functions within the script to complete your tasks. Depending on your local IS organization, your shell scripts may need to conform to specific specifications, such as comments and programming style. Therefore, the preceding code may need further modification before is can be put into production.

16.7 Quiz