Upload
shanna-greene
View
241
Download
3
Embed Size (px)
Citation preview
Shell Scripts
• A sequence of Unix commands
• Must be stored in Unix format (no <CR> character) file
• The file must be executable (use chmod u+x shellfile)
• To execute a shellfile: (type in the shell script filename) such asshellfile
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 1st 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
$@ quoted 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 command
Bourne Shells
• Change from Local to Environment Variable:export variable
• Example:
export myIncludeDir
• read variableRead a line of input and store in $variable
Bourne Shells • Evaluate Expression: expr expression
Expressions Explain
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
Signals Explain
1 or SIGHUP hang up (logout)
2 or SIGINT Press interrupt key
3 or SIGQUIT quit
9 or SIGKILL kill
Redirection
• Associate standard input channel with file descriptor n– command <& n
• Associate standard error channel with file descriptor n– command >& n
Where n: file descriptorn=0: standard inputn=1: standard outputn=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/nullThis 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>&1Then the syntax error will be stored in beep.out along with the regular output.
Here Document
• here document: • command << label
or command >> label
The shell copies standard input <<(or output >>) 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
• 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 1st 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 ... invalue1)...;;value2)...;;...*)...;;esac
Control Structures - while
• while structure
• Syntax: (another way)– while [condition]; do
...
...done
Bourne Shells Example #! /bin/sh
# Usage: gradehw 1If [ $# -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 1fi
Shell Module
• Example: – A Shell Module/Subprogram
RunGrading() {…
}– Execute the module in a shell script by
RunGrading
• Check Compilation Example test2.sh
Example of Stealing Superuser if superuser set . As 1st entry in search
path (p. 113 Practical Unix)• User: prepare shell script called ls in home
directory and do the following:cdchmod 700 .touch ./-f
• Superuser:sucd /home/userls
The superuser suggests to user not to use file name –f, didn’t know /bin/sh being stolen as a hidden file .steal