Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University
Objectives Understand how to use Bourne shell to write shell script
Shell Scripts A sequence of Unix commands Must be stored in Unix format (no character) file The file must be executable (use chmod u+x shellfile) To execute a shellfile: (type in the shell script filename) such as shellfile
Identify Shell Script If the first line has form: #!shellpath, then shellpath determines which shell the script file is interpreted. If the first line is just a #, then the current shell is used. other # form is a comment line (do not start the comment from 1st character of the 1st line, give at least a space if the comment line starts from 1 st line) Otherwise, Bourne shell is used as default shell.
Bourne Shells More Predefined Environment Vaiables: $PS1: shell prompt for 1st line of shell command $PS2: shell prompt for 2nd line or more of the continued shell command
Bourne Shells More Predefined Local Variables: Predefined Local Variables Explain list of all command line parameters (other than the command) $#total # of all command line parameters (don’t count command) $?exit value of the last command
Bourne Shells (Cont.) More Predefined Local Variables: Predefined Local Variables Explain $!PID of the last background command $$PID of the shell
Bourne Shells (Cont.) Example: Make sure finish of previous command before proceed to next commandMake sure finish of previous command before proceed to next command
Bourne Shells Assign value to a variable: variable=value Example: myIncludeDir = /usr/include
Bourne Shells Change from Local to Environment Variable: export variable Example: export myIncludeDir read variable Read a line of input and store in $variable
Bourne Shells Evaluate Expression: expr expression ExpressionsExplain string: regular_expression return string length if both sides match; return 0 otherwise
Signal - trap Execute command based on the signals received: –trap command signal1 signal2... The shell will execute the command if either one of the signals received. If signal =0, shell executes the command when the the shell script terminates.
Signals Explain 1 or SIGHUPhang up (logout) 2 or SIGINTPress interrupt key 3 or SIGQUITquit 9 or SIGKILLkill
Redirection Associate standard input channel with file descriptor n –command <& n Associate standard error channel with file descriptor n –command >& n Where n: file descriptor n=0: standard input n=1: standard output n=2: standard error
Redirection (Cont.) Example: –(find / -name handler.py > /dev/tty) > & /dev/null This finds the python file and sends errors to /dev/null (drop error message since /dev/null is a pseudo-device), and results to /dev/tty (one’s terminal), so you don’t see error in screen
Redirect Standard Error Redirect Standard Error (2>) –To an error file command 2> errorfile Example: –gcc beepsyntax.c > beep.out 2>beep.err Then the syntax error will be stored in beep.err –find / -name handler.py –print 2>/dev/null This will show the finding in screen without error messages
Redirect Standard Error (Cont.) Redirect Standard Error (2>) –To Standard Output: command > outputfile 2>&1 Example: gcc beepsyntax.c > beep.out 2>&1 Then the syntax error will be stored in beep.out along with the regular output.
Here Document here document: command > label The shell copies standard input >) up to, but not including the line with the label into the shell buffer and then execute the command. (Note: label must start a line somewhere down.)
Control Structures - if if structure Syntax: –if [condition] (or if test condition) then fi
Control Structures - if Condition: commonly used are as below -d dirfile: true if dirfile exists as a directory -f file: true if file exists as a regular file -r file: true if file exists as readable -x file: true if file exists as executable -s file: true if file contains at least one character str1 = str2 str1 != str2
Control Structures - if Condition: (Cont.) str1: true if str1 is not null (you can use this to check whether a process ID still exists) int1 -eq int2 int1 -ne int2 int1 -gt int2 int1 -ge int2 int1 -lt int2 int1 -le int2
Control Structures – if (Cont.) Condition: (Cont.) !expr expr1 -a expr2: and expr1 -o expr2: or \(expr\): grouping expression
Control Structures – if (Cont.) Example: –If [ -f as5.pl ] then perl as5.pl fi This checks if file as5.pl exists, then run the Perl program.
Control Structures – if (Cont.) Example: (another way) –If [ -f as5.pl ]; then perl as5.pl fi This checks if file as5.pl exists, then run the Perl program.
Control Structures – if (Cont.) Example: –If [ “$1” = “” ] then echo Usage: check.sh uid fi This checks if the 1 st command line argument exists. If not, display the error message by Usage clause.
Control Structures – if (Cont.) Example: –If [ ! -f as5.php ] then perl as5.pl fi This checks if file as5.php does not exist, then run the Perl program.
Control Structures if … elif if structure Syntax: –if [condition] (or if test condition) then... elif...(many times> then... else... fi
Control Structures - case case structure Syntax: –case... in value1)... ;; value2)... ;;... *)... ;; esac
Control Structures - while while structure Syntax: –while [condition] do done
Control Structures - while while structure Syntax: (another way) –while [condition]; do done
Control Structures - for for structure Syntax: –for variable in... do done
Control Structures - until until structure Syntax: –until... do done
Bourne Shells Example #! /bin/sh # Usage: gradehw 1 If [ $# -eq 1 ] then echo Enter a Grading System Program, Grade HW $1 cd hw$1 for i in `ls` do cd $i cp as$1.c as$1.txt vi as$1.txt cd.. done cd.. else echo Error: Usage: gradehw 1 fi
Shell Module Example: –A Shell Module/Subprogram RunGrading() { … } –Execute the module in a shell script by RunGrading Check Compilation Example test2.sh
More Examples: track.sh track.cleanup track.sed menu.sh
Example of Stealing Superuser if superuser set. As 1 st entry in search path (p. 113 Practical Unix) User: prepare shell script called ls in home directory and do the following:ls cd chmod 700. touch./-f Superuser: su cd /home/user ls The superuser suggests to user not to use file name –f, didn’t know /bin/sh being stolen as a hidden file.steal
Reference Ch. 5 Practical Unix & Internet Security by Garfinkel, Spafford & Schwartz, 2ed, O’Reilly