490 likes | 715 Vues
UNIX/Linux. Shell Programming. Shell Scripts. To pass parameters through the command line, codes are used when referring to the parameter in the body of the script file. Shell Scripts.
E N D
UNIX/Linux Shell Programming
Shell Scripts • To pass parameters through the command line, codes are used when referring to the parameter in the body of the script file.
Shell Scripts • Suppose that you are always typing the dir command (out of habit from DOS) when you mean to type ls –l and you want to pass a command line parameter to the script • The solution would be to create a script file named dirr that contained the following text: ls -l $1
Shell Scripts • Script Codes from the command line: $1 = 1st argument $2 = 2nd argument $3 = 3rd argument $* = all arguments $0 = name of the shell script $# = number of arguments
Shell Scripts Example: $ cat dirr ls -l $1 $
Shell Scripts Example: $ cat dirr ls -l $1 What happens now? $ dirr A*
Shell Scripts Another example: $ cat > rename mv $1 $2 And run it…. $ rename abc xyz
echo Command • Purpose • The echo statement writes character strings to standard output. • Syntax echo[ String ... ]
echo Command • Description • Writes character strings to standard output. • Strings are separated by spaces, and a new-line character follows the last String parameter specified. • If no String parameter is specified, a blank line (new-line character) is displayed.
echo Command • The echo command recognizes the following escape conventions: \b Displays a backspace character. \c Suppresses the new-line character that otherwise follows the final argument in the output. All characters following the \c sequence are ignored.
echo Command • The echo command recognizes the following escape conventions: \f Displays a form-feed character. \n Displays a new-line character. \r Displays a carriage return character.
echo Command • The echo command recognizes the following escape conventions: \t Displays a tab character. \v Displays a vertical tab character. \\ Displays a backslash character.
echo Command Example: $ cat > example1 echo Bad Filename $ $ ./example1 Bad Filename
echo Command Another example: $ cat > example2 echo –e "\n\nI'm at lunch.\nI'll be back at 1:00.“ $ ./example2 I'm at lunch. I'll be back at 1:00.
echo Command • Note: You must put the message in quotation marks if it contains escape sequences. And you must use the –e switch so echo will read the escape sequences
read Command • Purpose • The read statement reads one line from standard input and stores values in shell variables. • Syntax read [ VariableName ... ]
read Command • Description • Reads one line from standard input and assigns the values of each field in the input line to a shell variable using the characters in the IFS (Internal Field Separator) variable as separators.
read Command • Description • The VariableName parameter specifies the name of a shell variable that takes the value of one field from the line of input. • The first shell variable is assigned the value of the first field, the second shell variable specified is assigned the value of the second field, and so on, until the last field is reached.
read Command • Description • If the line of standard input has more fields than there are corresponding shell variables specified by the VariableName parameter, the last shell variable specified is given the value of all the remaining fields. • If there are fewer fields than shell variables, the remaining shell variables are set to empty strings.
read Command Example: $ cat > example3 echo ‘Please enter the name of the file to delete:\c’ read delfile rm $delfile echo $delfile Deleted $ ./example3 Please enter the name of the file to delete: xyz xyz Deleted $
for Command • Purpose • The for statement provides a simple method of looping. • Syntax forvariable in this list of values do allthe following commands until the ‘done’ statement done
for Command Example: $ for x in `ls` > do > echo Filename is $x > done
if Command • Purpose • The if statement provides conditional execution of statements. • Syntax ifthis command is successful thenexecute all these commands up to the ‘fi’ fi
if Command Example: if cd /users/smith then echo the smith directory exists fi
if Command • Purpose • The else statement adds an if-then-else construct to if statements.
if Command • Syntax if this command is successful thenexecute all these commands up to the ‘else’ elseexecute all these commands up to the ‘fi’ fi
if Command Example if cd /users/smith then echo the smith directory exists else echo the smith directory does not exist fi
test Command • Purpose • The test command provides additional testing in an if statement • There are 3 types of tests: • tests on numerical values • tests on file types • tests on character strings
test Command • Tests on numerical values • Syntax: test N <primitive> M • The primitives are: -eq -ne -gt -lt -ge -le
test Command Example: users=`who|wc -l` if test $users -gt 8 then echo “more than 8 people are logged on” fi
test Command Another example: if test $# -eq 0 then echo “You must give a file name after the command” else sort -k3,3 -k2,2 $1 fi
test Command • Tests on File Types • Syntax: test <primitive> filename
test Command • The most common primitives are: -s check that the file exists and is not empty -f check that the file is an ordinary file (not a directory) -d check whether the file is really a directory -w check that the file is writable -r check that the file is readable -x check that the file is executable
test Command Example: if test -d $1 then cd $1 ls -l cd else echo “$1 directory does not exist” fi
test Command • Tests on Character Strings • Syntax test String1 <primitive> String2 • The two primitives are: = test that the strings are equal != test that the strings are not equal
test Command Example: if test $LOGNAME != plotnicki then echo this command is restricted to john plotnicki else ... fi
test Command • Tests on Character Strings • Alternate Syntax test <primitive> String • The two primitives are: -z check if the string has zero length -n check if the string has non-zero length
test Command Example: if test -z “$1” then echo “You must input a parameter” else ... fi
test Command • Three operators can be used to combine or negate test: -o stands for the logical “or” -a stands for the logical “and” ! stands for negation
test Command Example: The following shell would test to see if we had write permission on thatfile and read permission on thisfile: if test -w $2 -a -r $1 then cat $1 >> $2 else echo cannot append fi
test Command An alternate form (and probably more readable) is to enclose the test in square brackets rather than using the word test. if [ -x /usr/bin/fortune ] then echo /usr/bin/fortune echo fi
while Command • Purpose • Provides a method of looping while a certain condition exists. • Syntax whilethis command is successful doall these commandsuntil the ‘done’ done
while Command Example: while test $# -gt 0 do sort -k3,3 -k2,2 $1 >> bigfile shift done • The shift command renumbers the arguments and decrements $#.
break and continue Commands • Purpose • The break statement causes the current loop to be terminated. • The continue statement causes the rest of the commands in the current iteration of the loop to ignored and the loop restarted for another iteration.
case Command • Purpose • The case statement provides an easy way to provide a menu of choices.
case Command • Syntax case string in string1) if ‘string’ is the same as ‘string1’ then execute all these commands until the‘;;’ ;; string2) if ‘string’ is the same as ‘string2’ then execute all these commands until the ‘;;’ ;; string3) ... etc... *) if none of the previous strings matched then execute all these commands until the ‘;;’ ;; esac
case Command echo Please enter the letter for the program you wish to execute: echo ‘a - For the current date’ echo ‘b - For who is logged on the system’ echo ‘c - For a list of your files’ read choice case $choice in a) date;; b) who;; c) ls;; *) echo That was not a valid choice;; esac