1 / 47

UNIX/Linux

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.

vesta
Download Presentation

UNIX/Linux

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. UNIX/Linux Shell Programming

  2. Shell Scripts • To pass parameters through the command line, codes are used when referring to the parameter in the body of the script file.

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

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

  5. Shell Scripts Example: $ cat dirr ls -l $1 $

  6. Shell Scripts Example: $ cat dirr ls -l $1 What happens now? $ dirr A*

  7. Shell Scripts Another example: $ cat > rename mv $1 $2 And run it…. $ rename abc xyz

  8. echo Command • Purpose • The echo statement writes character strings to standard output. • Syntax echo[ String ... ]

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

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

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

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

  13. echo Command Example: $ cat > example1 echo Bad Filename $ $ ./example1 Bad Filename

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

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

  16. read Command • Purpose • The read statement reads one line from standard input and stores values in shell variables. • Syntax read [ VariableName ... ]

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

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

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

  20. 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 $

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

  22. for Command Example: $ for x in `ls` > do > echo Filename is $x > done

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

  24. if Command Example: if cd /users/smith then echo the smith directory exists fi

  25. if Command • Purpose • The else statement adds an if-then-else construct to if statements.

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

  27. if Command Example if cd /users/smith then echo the smith directory exists else echo the smith directory does not exist fi

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

  29. test Command • Tests on numerical values • Syntax: test N <primitive> M • The primitives are: -eq -ne -gt -lt -ge -le

  30. test Command Example: users=`who|wc -l` if test $users -gt 8 then echo “more than 8 people are logged on” fi

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

  32. test Command • Tests on File Types • Syntax: test <primitive> filename

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

  34. test Command Example: if test -d $1 then cd $1 ls -l cd else echo “$1 directory does not exist” fi

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

  36. test Command Example: if test $LOGNAME != plotnicki then echo this command is restricted to john plotnicki else ... fi

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

  38. test Command Example: if test -z “$1” then echo “You must input a parameter” else ... fi

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

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

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

  42. while Command • Purpose • Provides a method of looping while a certain condition exists. • Syntax whilethis command is successful doall these commandsuntil the ‘done’ done

  43. 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 $#.

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

  45. case Command • Purpose • The case statement provides an easy way to provide a menu of choices.

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

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

More Related