16.5.5 Changing Your
Environment from the Command Line
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.
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:
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. |
chmod u=rwx myscript
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.
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:
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.
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.
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.
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.
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:
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.
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 |
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 |
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 |
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 |
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 |
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.
#!/usr/bin/ksh
# Script Name: mybackup # Author: I.T. Wasme # This script will run a backup.
# Set variables here DAILY=`date +%a` MONTHLY=`date +%d` BYE=stay
# Start Script Here
while [ $BYE = stay ] do
for i in rootvg uservg tempvg do # Interactive Prompting.
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
case "$inp" in y*|Y*) echo Backup will Run BYE=stay ;; n*|N*|*) echo Backup will Not Run
BYE=go
esac
fi
if [ $BYE = stay ] then # Backup Section Starts here echo "\nBackup procedure starting"
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
fi else
BYE=go
fi
done
# Set single exit point
if [ $BYE = go ] then echo Backup procedure complete
exit
fi
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.