Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Bourne Shells Scripts

Similar presentations


Presentation on theme: "Chapter 5 Bourne Shells Scripts"— Presentation transcript:

1 Chapter 5 Bourne Shells Scripts
By C. Shing ITEC Dept Radford University

2 Objectives Understand how to use Bourne shell to write shell script

3 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 as shellfile

4 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.

5 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

6 Bourne Shells More Predefined Local Variables: Predefined Local
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

7 Bourne Shells (Cont.) More Predefined Local Variables: Predefined
Explain $! PID of the last background command $$ PID of the shell

8 Bourne Shells (Cont.) Example:
Make sure finish of previous command before proceed to next command

9 Bourne Shells Assign value to a variable: variable=value Example:
myIncludeDir = /usr/include

10 Bourne Shells Change from Local to Environment Variable: export variable Example: export myIncludeDir read variable Read a line of input and store in $variable

11 Bourne Shells Evaluate Expression: expr expression Expressions Explain
string: regular_expression return string length if both sides match; return 0 otherwise

12 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.

13 Signals Signals Explain 1 or SIGHUP hang up (logout) 2 or SIGINT
Press interrupt key 3 or SIGQUIT quit 9 or SIGKILL kill

14 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

15 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

16 Redirect Standard Error
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

17 Redirect Standard Error (Cont.)
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.

18 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.)

19 Control Structures - if
if structure Syntax: if [condition] (or if test condition) then fi

20 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

21 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

22 Control Structures – if (Cont.)
Condition: (Cont.) !expr expr1 -a expr2: and expr1 -o expr2: or \(expr\): grouping expression

23 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.

24 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.

25 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.

26 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.

27 Control Structures if … elif
if structure Syntax: if [condition] (or if test condition) then ... elif ...(many times> then ... else ... fi

28 Control Structures - case
case structure Syntax: case ... in value1) ... ;; value2) ... ;; ... *) ... ;; esac

29 Control Structures - while
while structure Syntax: while [condition] do done

30 Control Structures - while
while structure Syntax: (another way) while [condition]; do done

31 Control Structures - for
for structure Syntax: for variable in ... do done

32 Control Structures - until
until structure Syntax: until ... do done

33 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 else echo Error: Usage: gradehw 1 fi

34 Shell Module Example: RunGrading() { } RunGrading
A Shell Module/Subprogram RunGrading() { } Execute the module in a shell script by RunGrading Check Compilation Example test2.sh

35 More Examples: track.sh track.cleanup track.sed menu.sh

36 Example of Stealing Superuser if superuser set
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: 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

37 Reference Ch. 5 Practical Unix & Internet Security by Garfinkel, Spafford & Schwartz, 2ed, O’Reilly


Download ppt "Chapter 5 Bourne Shells Scripts"

Similar presentations


Ads by Google