Download presentation
Presentation is loading. Please wait.
Published byBlanche Riley Modified over 9 years ago
1
Shell Programming
2
An example u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg file2 –encrypts file1 into file2 u record this command with a script file –myencrypt #!/bin/sh tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg –mydecrypt #!/bin/sh tr thequickbrownfxjmpsvalzydg \ abcdefghijklmnopqrstuvwxyz
3
An example u chmod the files to be executable % chmod u+x myencrypt mydecrypt u run them as normal commands %./myencrypt file2 %./mydecrypt file3 % diff file1 file3
4
Shell Scripts (1) u A Shell Script is simply a text file with UNIX commands in it u Shell Scripts usually begin with a #! and a shell name –For example:#!/bin/sh –If they do not, the user's current shell will be used u Any UNIX command can go in a Shell Script –these commands are executed in order or in the flow determined by the control statements –much like a program in a language like Pascal, C or Java u Different shells have different control structures –the #! line is very important –we will generally write shell scripts with the Bourne (sh) shell
5
Shell Scripts (2) u Why Write Shell Scripts? –to avoid repetition v if you do a sequence of steps with standard UNIX commands over and over, why not just type one command? –to automate difficult tasks v many commands have subtle and difficult options which you don't want to figure out every time
6
Command Line Arguments (1) u Shell Scripts would not be much useful if we could not pass arguments to them on the command line u Shell Script arguments are "numbered" from left to right –Inside the script refer to each argument by this number with a $ in front v $1 - first argument after command v $2 - second argument after command v... up to $9 –These are called Positional Parameters and can be used much like shell variables
7
Command Line Arguments (1) u Example 1: get a particular line of a file –Write a command with the format: getlineno linenumber filename #!/bin/sh head -$1 $2 | tail –1 u Other variables related to arguments v $0 name of the command running v $* All the arguments (even if there are more than 9) v $# the number of arguments
8
Command Line Arguments (2) u Example 2: print the oldest files in a directory #! /bin/sh # oldest -- examine the oldest parts of a directory HA=$1 shift ls -lt $* | tail +2 | tail $HA u The shift command shifts all the arguments to the left –$1 = $2, $2 =$3, $3 = $4,... –$1 is lost (but we have saved it in $HA) –useful when there are more than 9 arguments u The "tail +2" command removes the first line (why do this?)
9
Bourne Shell Variables (1) u There are three basic types of shell variables –Positional variables v $1, $2, $3,..., $9 -- we've seen these –Keyword variables v like PATH, HA, and anything else we may define –Special variables v $*, $#-- all the arguments, the number of the arguments v $$-- the process id of the current shell v $?-- return value of last foreground process to finish -- more on this one later v and others you can find out about with man sh
10
Bourne Shell Variables (2) u Bourne shell variables are much like variables in a standard programming language –Examples: PATH=$PATH:$HOME/bin HA=$1 PHRASE="House on the hill" v if the value you wish to assign has spaces, RHS (right hand side) must be quoted v if you want sub-shells and commands to be able to look at these variables, you must export them export PATH HA PHRASE Note: no space around =
11
Bourne Shell Variables (3) u It is also sometimes useful to do numerical computations with shell variables –like everything else in UNIX this is done with a command: expr –expr evaluates very simple expressions of constants –For example: obelix[1]% expr 3 + 4 7 obelix[2]% expr \( 3 + 4 \) \* 5 % 6 5 –Results go to standard output v how do you use the results? note: all operands and operators are separated by spaces note: all special chars must be quoted or escaped
12
Bourne Shell Variables (4) u shell variables are expanded first, then the operands are given to the shell obelix[3]% a=5 obelix[4]% expr $a + 1 6 u Use results of expr with backquotes: `...` obelix[5]% a=`expr \( $a + 1 \)` obelix[6]% echo $a 6 u expr supports the following operators: –arithmetic operators: +,-,*,/,% –comparison operators: =, > –boolean/logical operators: &, | –parentheses: (, ) –precedence is the same as C, Java
13
Control Statements (1) u Control statements control the flow of execution in an imperative programming language –this is how Java, C, C++,.... and Shells work –non-imperative languages like lisp, ML, Prolog, Miranda,... may work differently u Without control statements execution within a shell scripts executes one statement after the other –each statement may be quite complicated and may invoke other UNIX commands (or even the same command...) u The three most common types of control statements: –conditionals: if/then/else, switch, case,... –loop statements: while, for, repeat, do,... –branch statements: subroutine calls (good), goto (bad)
14
Control Statements (2) u Both loops and conditions generally "test" something –essentially comes down to a binary decision u In a Bourne shell script, the only thing you can test is whether or not a command is "successful" –every well behaved command returns back a return code v 0 if it was successful v non-zero if it was unsuccessful (actually 1..255) u examine the return code of the previous UNIX command executed in a shell with $? obelix[7]% grep “bma" < /dev/null obelix[8]% echo $? 1 obelix[9]% echo “bma" | grep “bma" obelix[10]% echo $? 0
15
Conditional (1) The if statement u Simple form: if decision_command_1 then command_set_1 fi u Example: if grep “bma“ $1 >/dev/null then echo -n "It\'s" echo "there" fi grep returns 0 if it finds something returns non-zero otherwise redirect to /dev/null so that "intermediate" results do not get printed
16
Conditional (2) u Else form: if decision_command_1 then command_set_1 else command_set_2 fi u Example: if grep "UNIX" myfile >/dev/null then echo "UNIX occurs in file" else echo "UNIX does not occur in file" fi The indentation used in these examples is "optional" (as in most programming languages) Good indentation is very highly recommended Required for cs211 assignments
17
Conditional (3) u Elif form: if decision_command_1 then command_set_1 elif decision_command_2 command_set_2... fi u Example: if grep "UNIX" myfile >/dev/null then echo "UNIX occurs in file" elif grep "DOS" myfile >/dev/null echo “Unix does not occur, but DOS does" else echo “nobody is there" fi
18
Conditional (4) u Alternatively, statements can be separated by a semicolon (;) –For example: if grep "UNIX" myfile; then echo "Got it"; fi –This actually works anywhere in the shell. v E.g. obelix[11]% cwd=`pwd`; cd $HOME; ls; cd $cwd
19
Conditional (5) u Sometimes it is useful to have a command which does "nothing" u The : (colon) command in UNIX does nothing besides having its parameters expanded (and ignored). –it returns a return code of 0 u Example: #!/bin/sh # pgrep: a polite grep if grep $* then : else echo "Sorry, but '$1' was not there..." fi
20
The test Program (1) u File tests: test –f file v does file exist and is not a directory? test –x file v does file exist and is executable? test –s file v does file exist and is longer than 0 bytes? test –z string v is string of length 0 (i.e., the null string)? v See man test(1) for more options u For example you can write: #!/bin/sh # saferm if test –f $1 then mv $1 $1.`date '+%Y-%M-%d:%H:%M.%S'` fi gives the date as 1999-10-7:9:30.23
21
The test Program (2) u String tests –test can compare strings test string1 = string2v does string1 equal string2? test string1 != string2v does string1 not equal string2? –For example, you might do something different depending upon your TERM variable: if test $TERM = vt100; then stty erase '^?' resize elif test –z $TERM; then stty erase '^H' fi stty configures various keys on the keyboard (you can look up details)
22
The test Program (3) u Integers can also be compared –use –eq, -ne, -lt, -le, -gt, -ge u For example: #!/bin/sh # smallarg: print the smallest line args if test $# -eq 1; then echo $1 elif test $# -gt 1; then A=$1 shift B=`smallarg $*` if test $A -le $B; then echo $A else echo $B fi Recursion: Cooooool
23
The test Program (4) u The test program has an alias as [ –add in a matching ] at the end –this is supposed to be a bit easier to read #!/bin/sh # smallarg: print the smallest line args if [ $# -eq 1 ]; then echo $1 elif [ $# -gt 1 ]; then A=$1; shift B=`smallarg $*` if test $A -le $B; then echo $A else echo $B fi
24
The test Program (1) u File tests: test –f file v does file exist and is not a directory? test –x file v does file exist and is executable? test –s file v does file exist and is longer than 0 bytes? test –z string v is string of length 0 (i.e., the null string)? v See man test(1) for more options u For example you can write: #!/bin/sh # saferm if test –f $1 then mv $1 $1.`date '+%Y-%M-%d:%H:%M.%S'` fi gives the date as 1999-10-7:9:30.23
25
The test Program (2) u String tests –test can compare strings test string1 = string2v does string1 equal string2? test string1 != string2v does string1 not equal string2? –For example, you might do something different depending upon your TERM variable: if test $TERM = vt100; then stty erase '^?' resize elif test –z $TERM; then stty erase '^H' fi stty configures various keys on the keyboard (you can look up details)
26
The test Program (3) u Integers can also be compared –use –eq, -ne, -lt, -le, -gt, -ge u For example: #!/bin/sh # smallarg: print the smallest line args if test $# -eq 1; then echo $1 elif test $# -gt 1; then A=$1 shift B=`smallarg $*` if test $A -le $B; then echo $A else echo $B fi Recursion: Cooooool
27
The test Program (4) u The test program has an alias as [ –add in a matching ] at the end –this is supposed to be a bit easier to read #!/bin/sh # smallarg: print the smallest line args if [ $# -eq 1 ]; then echo $1 elif [ $# -gt 1 ]; then A=$1; shift B=`smallarg $*` if test $A -le $B; then echo $A else echo $B fi
28
Command Line Input u The read command reads one line of input from the terminal and assigns it to variables give as arguments u Syntax read var1 var2 var3... v action: reads a line of input from standard input v assign var1 to first word, var2 to second word,... v the last variable gets any excess variables on the line u Example: obelix[11]% read X Y Z Here are some words as input obelix[12]% echo $X Here obelix[13]% echo $Y are obelix[14]% echo $Z some words as input
29
The While Loop (1) u While loops repeat statements while the return code of some UNIX command is 0 u The syntax is: while decision command do command_set done u Example 1: #!/bin/sh # print numbers from 1 to $1 COUNT=1 while [ $COUNT –le $1 ]; do echo –n $COUNT " " COUNT=`expr $COUNT + 1` done echo
30
The While Loop (2) u Example 2: –a more conventional approach to finding the smallest argument #!/bin/sh # smallarg: print the smallest line args smallest=$1 shift while [ $# -gt 0 ]; do if [ $1 –lt $smallest ]; then smallest=$1 fi shift done echo $smallest
31
For Loops (1) u For loops allow the repetition of a command for a specific set of values –E.g.: each argument on a line, each file in a set of files u Unlike many programming languages, for loops in shell script are not numeric or counted –we can use a while loop for this u Syntax: for var in value1 value2... do command_set done –command_set is executed with each value of var (value1, value2,...) in sequence
32
For Loops (2) u For Loop Example 1: #!/bin/sh # timestable – print out a multiplication table echo –n "Which times table do you want?" read Table for i in 1 2 3 4 5 6 7 8 9 10 do Value=`expr $i \* $Table` echo $i x $Table = $Value done
33
For Loops (3) u For Loop Example 2: #!/bin/sh # file-poke – tell us stuff about files for file in $*; do echo –n "$file " if [ -s "$file" ]; then echo is full. else echo is empty. fi done –command goes through all files on the command line v these could be from filename expansion – we might type obelix[16]% file-poke * v Inside file-poke, $* will consist of a list of all files in the current directory
34
For Loops (4) u For Loop Example 3: #!/bin/sh # findcmd – show where command $1 is found in $PATH for prog in $*; do for dir in `echo $PATH | tr ':' ' '`; do if [ -x $dir/$prog ]; then echo $dir/$prog fi done PATH is : separated convert to space separated Note: doubly nested loop
35
Piping Input to a Loop u The standard input can be piped into a while or for loop –take a look at the output from the who command: obelix[15]% who watt ttyp3 Aug 27 11:30 (aquarium.scl.csd.uwo.ca) watt ttyp4 Sep 30 21:19 (diamond.admin.csd.uwo.ca) mwg ttyp2 Oct 6 16:57 (heffalump.csd.uwo.ca) –Now write a command to make this output "friendly": #!/bin/sh who | while read user tty month date time therest do echo –n $user is on $tty and has been since $month $date at $time. if [ ! –z "$therest" ]; then echo " $user is connected from $therest" else echo fi done Pipe output of who into the while loop read reads one line at a time from the standard input returns non-zero user code at end-of-file
36
Piping Output from a loop u The output from a loop can also be piped into the next command u For example: #!/bin/sh # findcmd2 – show where command $1 is found in $PATH # – and print extended information about it for prog in $*; do for dir in `echo $PATH | tr ':' ' '`; do if [ -x $dir/$prog ]; then echo $dir/$prog fi done done | while read cmd; do ls –l $cmd done; Output of for loop is piped into while loop, where it is read, one line at a time
37
Piping In and Out of a Loop u A more complex example: #!/bin/sh # lookfor – watch who output for specific users who | while read user tty mon dt year therest; do for watch in $*; do if [ $watch = $user ]; then echo $watch is on fi done done | sort | uniq –c | sed –e 's/ *\([0-9][0-9]*\) \(.*\)/\2 \1 times/' make lines unique but include count grab count into \1 re-order for printout
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.