COMP 4/6262: Programming UNIX Lecture 12 shells, shell programming: passing arguments, if, debug March 13, 2006
Outline shells shell programming passing arguments (KW Ch.7) exit status if (KW Ch.8) test debug
the relationship of shell functionality Korn Shell Bourne Shell common core C Shell common core
core shell functionality built-in commands scripts variables (local, environment) redirection wildcards pipes sequences subshells background processing command substitution
common shells the shell you use is a matter of taste, power, compatibility, and availability C shell is better than Bourne shell for interactive work, but slightly worse in some respects for script programming Korn shell incorporates the best of features of the Bourne shell and C shells Bash also best of all worlds, includes features from all the other major shells Bourne shell comes with every version of UNIX Bash is the newest, is becoming the most popular, open software
Bash Bash Bourne Again Shell Backward compatibility with Bourne Shell Includes most useful features from C Shell and Korn Shell Shell of choice for Linux Bash Specifics Variable manipulation Command-line processing, aliases, and history Arithmetic, conditional expressions, control structures Directory stack Job control Shell functions
changing shell chsh tcsh ksh
So Many Commands Unix has a lot of commands, but there is no way it has everything What do you do if no command exists that does what you want? Build it yourself! The shell itself is actually a programming language A shell program/script consists of a sequential list of commands think of it as a listing of programs to run in order
Ways To Run Shell Executables source FILENAME /bin/bash FILENAME Make the file executable and run it as a program First line should specify which shell/interpreter to use #!/bin/bash #!/usr/bin/perl
Example Check whether a user is logged on
Command Line Parameters Just like all other Unix programs, shell scripts can read parameters off of the command line Different from user input Value is known before execution, not typed in during execution Example: >commandsfile.sh param1.test
Passing Arguments Accessing command line parameters requires special variables $0 The name of the running program $1-$9 The first nine arguments to the program $* All of the command line arguments $# The total number of command line arguments
Example Check whether a user is logged on Command file ison who grep $1 Usage./ison 6262-00
Shift only first 9 arguments are directly accessible via $1, $2,, $9 for 10 or above use ${10}, ${11}, etc. if more are supplied use shift command to access them Shifts the arguments to the left and brings new values in from the right $1=$2 $2=$3 $9=${10} $# is automatically decremented by one example: testshift
Visual Representation Of shift $1 $2 $3 $4 $5 $6 $7 $8 $9 a b c d e f g h i j shift b c d e f g h i j $1 $2 $3 $4 $5 $6 $7 $8 $9
exit status Every program and shell returns an integer value to indicate success or failure 0 - Success Any other number Failure Used to inform other programs how things went
exit status variable $? the exit status of the last command executed in a pipeline, the exist status is that of the last command in the pipe examples who grep 6262-00 echo $? who grep good echo $? echo $?
Control Flow And Conditionals Shell scripts can be more powerful than just a list of commands Sometimes you want to perform some commands only if certain conditions are true Example: Only print out a file if it is not a binary file
The test Command built-in shell command to check if certain conditions in an if command Usage: test expression test evaluate expression if the result is true, returns an exit status of 0 otherwise, the result is false, returns a nonzero exit status
Shortcut For Test Because this is used very often, a shortcut exists [] [ is the name of the command still initiates execution of the same test command, only that test expects to see a closing ] spaces after [ and before ] example test f tfile [ -f tfile ]
test operators (1) string operators string1 = string2 string1!= string2 string (return 0 if string is not null) -n string -z string (return 0 if string is null) integer operators i1 eq i2 e.g. $count eq 0 i1 ne i2 e.g. $# ne 0 i1 lt i2 i1 le i2 i1 gt i2 i1 ge i2
test examples x1= 005 x2= 10 [ $x1 = 5 ] echo $? [ $x1 -eq 5 ] [ $x2 = 10 ] [ $x2 -eq 10 ] name= test $name = 6262-00 test $name = 6262-00
test operators (2) file operators -d file exit status is 0 if file is a directory -e file exit status is 0 if file exists -f file exit status is 0 if file is a normal file -s file exit status is 0 if file is non-empty -x file exit status is 0 if file is executable
test operators (3)! Not Negates the next check Example: test! x tfile # if the file exists as exec -a And two conditions Example: test $1 eq $2 a $2 gt 5 -o Or two conditions Example: test $1 eq $2 o $2 gt 5
When Do You Use This? test is used to control the operation of your script The answer from test should guide the execution of your code one way or another Used in conditional statements
if if commandt then command command fi commandt is executed and its exit status is tested if exit 0, the commands between then and fi are executed otherwise, they are skipped
Example Edit a file safecat.sh with #!/bin/bash if [! x $1 ] ; then cat $1 fi How to run? At prompt type./safecat.sh filename
else if commandt then commands-yes else commands-no fi
Example Edit a file safecat.sh with #!/bin/bash if [! x $1 ] ; then cat $1 else echo File $1 is an executable: not printing! fi How to run? At prompt type./safecat.sh filename
elif if command1 then commands-1 elif command2 then commands-2 elif commandn then commands-n else commands fi
case case value in pattern1) commands-1 ;; pattern2) commands-2 ;; esac value is successively compared against the values pattern1, until a match is found the commands listed after the matching value, up to ;; are executed execution of case is terminated if a match is not found, none of the commands is executed
Example Edit a file casescript.sh #!/bin/bash case $1 in *.sh) echo Shell Script ;; *.txt) echo Text File ;; *) echo Some Other File ;; esac
Exercise Check whether a user is logged on ison who grep $1 on if the user is logged on, print.. is logged on otherwise print.. is not logged on check the number of arguments
Exercise print the type of the single character given as an argument: digits, uppercase letters, lowercase letters, special characters. add check: make sure just a single char is given as the argument ctype
Debug using shell s x option sh x programname arguments trace the execution of the program start a new shell to execute the program commands are printed at the terminal as they are executed, preceded by a +