Chapter 5 Bourne Shells Scripts

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

description

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 - PowerPoint PPT Presentation

Transcript of Chapter 5 Bourne Shells Scripts

Page 1: Chapter 5 Bourne Shells Scripts

Chapter 5 Bourne Shells Scripts

By C. Shing

ITEC Dept

Radford University

Page 2: Chapter 5 Bourne Shells Scripts

Objectives

• Understand how to use Bourne shell to write shell script

Page 3: Chapter 5 Bourne Shells Scripts

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

Page 4: Chapter 5 Bourne Shells Scripts

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.

Page 5: Chapter 5 Bourne Shells Scripts

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

Page 6: Chapter 5 Bourne Shells Scripts

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

Page 7: Chapter 5 Bourne Shells Scripts

Bourne Shells (Cont.)• More Predefined Local Variables:

Predefined

Local

Variables

Explain

$! PID of the last background command

$$ PID of the shell

Page 8: Chapter 5 Bourne Shells Scripts

Bourne Shells (Cont.)

• Example:

Make sure finish of previous command before proceed to next command

Page 9: Chapter 5 Bourne Shells Scripts

Bourne Shells

• Assign value to a variable:variable=value

• Example:myIncludeDir = /usr/include

Page 10: Chapter 5 Bourne Shells Scripts

Bourne Shells

• Change from Local to Environment Variable:export variable

• Example:

export myIncludeDir

• read variableRead a line of input and store in $variable

Page 11: Chapter 5 Bourne Shells Scripts

Bourne Shells • Evaluate Expression: expr expression

Expressions Explain

string: regular_expression

return string length if both sides match; return 0 otherwise

Page 12: Chapter 5 Bourne Shells Scripts

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.

Page 13: Chapter 5 Bourne Shells Scripts

Signals

Signals Explain

1 or SIGHUP hang up (logout)

2 or SIGINT Press interrupt key

3 or SIGQUIT quit

9 or SIGKILL kill

Page 14: Chapter 5 Bourne Shells Scripts

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

Page 15: Chapter 5 Bourne Shells Scripts

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

Page 16: Chapter 5 Bourne Shells Scripts

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

Page 17: Chapter 5 Bourne Shells Scripts

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.

Page 18: Chapter 5 Bourne Shells Scripts

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

Page 19: Chapter 5 Bourne Shells Scripts

Control Structures - if

• if structure

• Syntax:– if [condition] (or if test condition)then......fi

Page 20: Chapter 5 Bourne Shells Scripts

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

Page 21: Chapter 5 Bourne Shells Scripts

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

Page 22: Chapter 5 Bourne Shells Scripts

Control Structures – if (Cont.)

• Condition: (Cont.)

• !expr

• expr1 -a expr2: and

• expr1 -o expr2: or

• \(expr\): grouping expression

Page 23: Chapter 5 Bourne Shells Scripts

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.

Page 24: Chapter 5 Bourne Shells Scripts

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.

Page 25: Chapter 5 Bourne Shells Scripts

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.

Page 26: Chapter 5 Bourne Shells Scripts

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.

Page 27: Chapter 5 Bourne Shells Scripts

Control Structures if … elif

• if structure• Syntax:

– if [condition] (or if test condition)then...elif ...(many times>then...else...fi

Page 28: Chapter 5 Bourne Shells Scripts

Control Structures - case

• case structure• Syntax:

– case ... invalue1)...;;value2)...;;...*)...;;esac

Page 29: Chapter 5 Bourne Shells Scripts

Control Structures - while

• while structure

• Syntax:– while [condition]do......done

Page 30: Chapter 5 Bourne Shells Scripts

Control Structures - while

• while structure

• Syntax: (another way)– while [condition]; do

...

...done

Page 31: Chapter 5 Bourne Shells Scripts

Control Structures - for

• for structure

• Syntax:– for variable in ...do......done

Page 32: Chapter 5 Bourne Shells Scripts

Control Structures - until

• until structure

• Syntax:– until ...do......done

Page 33: Chapter 5 Bourne Shells Scripts

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

Page 34: Chapter 5 Bourne Shells Scripts

Shell Module

• Example: – A Shell Module/Subprogram

RunGrading() {…

}– Execute the module in a shell script by

RunGrading

• Check Compilation Example test2.sh

Page 35: Chapter 5 Bourne Shells Scripts

More Examples:

• track.sh

• track.cleanup

• track.sed

• menu.sh

Page 36: Chapter 5 Bourne Shells Scripts

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

Page 37: Chapter 5 Bourne Shells Scripts

Reference

• Ch. 5

• Practical Unix & Internet Security by Garfinkel, Spafford & Schwartz, 2ed, O’Reilly