1 / 37

Chapter 5 Bourne Shells Scripts

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 <CR> character) file

Download Presentation

Chapter 5 Bourne Shells Scripts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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:

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

  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 variableRead a line of input and store in $variable

  11. Bourne Shells • Evaluate Expression: expr expression

  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

  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 • Redirect Standard Error (2>) • To an error file • command 2> errorfile Example: • gcc beepsyntax.c > beep.out 2>beep.errThen 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.) • 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.

  18. Here Document • here document: • command << labelor 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 ... invalue1)...;;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 cd .. else echo Error: Usage: gradehw 1 fi

  34. Shell Module • Example: • 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 . 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

More Related