Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. 2 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a.

Similar presentations


Presentation on theme: "1. 2 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a."— Presentation transcript:

1 1

2 2

3 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a repetitive procedure 3

4 4

5 Parameters and Variables  A shell parameter is associated with a value that is accessible to the user. Shell variables Names consist of letters, digits and underscores By convention, environment variables use UPPERCASE Names must start with a letter or underscore User created variables (create and assign value) Keyword shell variables Have special meaning to the shell Being created and initialized by the startup file 5

6 Parameters and Variables Positional parameters Allow you to access command line arguments Special parameters Such as The name of last command The status of most recently executed command The number of command-line arguments 6

7 Shell variables To assign a value to a variable in the Bourne Again Shell, use the following syntax: VARIABLE=value myvar=abc Example person=alex echo person person echo $person alex 7

8 Shell variables The $VARIABLE syntax is a special case of the more general syntax ${VARIABLE}. Braces are necessary when concatenating a variable value with a string $ PREF=counter $ WAY=${PREF}clockwise $ FAKE=${PREF}feit $ echo $WAY $FAKE counterclockwise counterfeit 8

9 Shell variables Differences between “ ”, ‘ ’, \: $ echo $person alex $ echo "$person" alex $ echo '$person' $person $ echo \$person $person 9

10 Shell variables Read : Using read, scripts can accept input from the user and store that input in variables $ read program abc $ echo $program When you do not specify a variable to receive read’s input, bash puts the input into the variable named REPLY. $ read abc $ echo $REPLY 10

11 Some useful commands unset: Removes a Variable $ unset person readonly: Makes the Value of a Variable Permanent $ readonly person declare : Assign Attributes to Variables Examples : $ declare -x -r person3=helen $ declare +x person3 $ declare –x / declare -r 11

12 Array Variables The Bourne Again Shell supports one-dimensional array variables. The subscripts are integers with zero-based indexing (i.e., the first element of the array has the subscript 0). Syntax : name=(element1 element2...) $ NAMES=(max helen sam zach) Refer to a single element of an array as follows: $ echo ${NAMES[2]} 12

13 Locality of Variables You can have both global environment and local shell variables.  Global environment variables are set by your login shell and new programs and shells the environment of their parent shell.  Local shell variables are used only by that shell and are not passed on to other processes. A child process cannot pass a variable back to its parent process. 13

14 Locality of Variables Once you use the export builtin with a variable name as an argument, the shell places the value of the variable in the calling environment of child processes. This call by value gives each child process a copy of the variable for its own use 14

15 Locality of Variables Example $ cat > extest1 cheese=american echo "extest1 1: $cheese" bash./subtest echo "extest1 2: $cheese" $ cat > subtest echo "subtest 1: $cheese" cheese=swiss echo "subtest 2: $cheese" $ bash./extest1 extest1 1: american subtest 1: subtest 2: swiss extest1 2: american $ cat > extest2 export cheese=american echo "extest1 1: $cheese" bash./subtest echo "extest1 2: $cheese" $ bash./extest2 extest1 1: american subtest 1: american subtest 2: swiss extest1 2: american 15

16 Global variables Some global environment variables are, HOME Path to your home directory HOST The hostname of your system LOGNAME The name you login with PATH Paths to be searched for $ PATH=/usr/local/bin:/bin:/usr/bin:~/bin: $ PATH=$PATH:. 16

17 Alias An alias is a name that the shell translates into another name or (complex) command. They are typically placed in the ~/.bashrc startup files The syntax of the alias builtin is alias [name[=value]] Example $ alias ls='ls -F‘ Avoid alias substitution by preceding the aliased command with a backslash (\): $ \ls 17

18 Alias Remove an alias with the unalias built-in. $ unalias zap $ alias command list all aliases 18

19 Using a Default Value The :– modifier uses a default value in place of a null or unset variable while allowing a nonnull variable to represent itself : ${name:–default} Example: ls ${LTI:-/home/oslab} The := modifier uses assign a default value to a null variable : ${name:=default} Example: ls ${LTI:=/home/oslab} 19

20 Positional Parameters  The command name and arguments are the positional parameters. Because you can reference them by their position on the command line $0 : Name of the calling program $1 - $9 : Command-line Arguments The first argument is represented by $1 The second argument is represented by $2 And so on up to $9 The rest of arguments have to be shifted to be able to use $1- $9 parameters. For values of n over 9, the number must be enclosed within braces -> ${12} 20

21 Positional Parameters Examples $ cat > abc echo "The command used to run this script is $0" $ bash./abc The command used to run this script is./abc Example $ cat > display_5args echo “First 5 arguments are $1 $2 $3 $4 $5” $ bash./display_5args 1 2 3 4 5 First 5 arguments are 1 2 3 4 5 21

22 Function Functions are defined similar to C and Java You can declare a shell function in the ~/.bash_profile startup file An example of a function function hello() { echo hello world! } Usage : $ hello 22

23 Function An example of a function function print () { echo $1 echo $2 } No parameter is defined in the deceleration of function Usage : $ print 1 2 1 2 23

24 Function Because functions run in the same environment as the shell that calls them, variables are implicitly shared by a shell and a function it calls. $ function nam () { > echo $myname > myname=zach > } $ myname=sam $ nam sam $ echo $myname zach 24

25 Locality of function variables The typeset built-in declares a variable to be local to the function it is defined in. $ function count_down () { > typeset count > count=$1 > while test $count -gt 0 > do > echo "$count..." > ((count=count-1)) > sleep 1 > done > echo "Blast Off." > } $ count=10 $ count_down 4 4... 3... 2... 1... Blast Off. $ echo $count 10 25

26 Shift  $1-$9 allows you to access 10 arguments How to access others?  Promote command-line arguments: shift Built-in command shift promotes each of the command- line arguments. The first argument ( which was $1) is discarded The second argument ( which was $2) becomes $1 The third becomes the second And so on Makes additional arguments available Repeatedly using shift is a convenient way to loop over all the command-line arguments 26

27 Shift  Example: $ cat > demo_shift #!/bin/tcsh echo $1 $2 $3 shift echo $1 $2 shift echo $1 $ bash./demo_shift 1 2 3 1 2 3 2 3 3 $ bash./demo_shift 1 2 3 1 2 3 2 3 3 27

28 Special Parameters  The number of arguments: $# Return a decimal number Use the test to perform logical test on this number Example : $ cat > num_args echo "This script was called with $# arguments." $ bash./num_args sam max zach This script was called with 3 arguments. 28

29 Special Parameters  Exit status: $? When a process stops executing for any reason, it returns an exit status to its parent process. By convention, Nonzero represents a false value that the command failed. A zero value is true and means that the command was successful Example : $ ls xxx ls: xxx: No such file or directory $ echo $? 1 29

30 Special Parameters  Value of Command-line arguments: $* and $@ $* and $@ represent all the command_line arguments ( not just the first nine) $* : treats the entire list of arguments as a single argument $@ : produce a list of separate arguments (Only bash/ksh/sh) Example : $ cat > display_all echo All arguments are $* $ bash./display_all a b c d e f g h i j k l m n o p All arguments are a b c d e f g h i j k l m n o p 30

31 Special Parameters  The PID: $$ $echo $$  $! : The value of the PID number of the last process that you ran in the background is stored in $! 31

32 32

33 Expression The expr command: VALUE=`expr VALUE \* 10 + NEW` The let command $ let "VALUE=VALUE * 10 + NEW“ $ let "VALUE=VALUE *10+NEW (without space) Bash accepts ((expression)) as a synonym for let "expression", obviating the need for both quotation marks and dollar signs: ((VALUE=VALUE * 10 + NEW)) 33

34 Notes on expr Why do we need the expr command ??? E.g: $ file=1+2 $echo $file 1+2 NOTE: 1+2 is copied as it is into val and not the result of the expression, to get the result, we need expr. $ file=`expr 1 + 2` or $ let “file=1 + 2” 34

35 Notes on expr expr supports the following operators: arithmetic operators: +,-,*,/,% comparison operators: =, > boolean/logical operators: &, |,! parentheses: (, ) precedence is the same as C, Java Use [[expr]] when using expression as a condition if [[ 30 < $age && $age < 60 ]] 35

36 String Pattern Matching The syntax for these operators is ${varname op pattern} Example : $ SOURCEFILE=/usr/local/src/prog.c $ echo ${SOURCEFILE#/*/} local/src/prog.c $ echo ${SOURCEFILE##/*/} prog.c $ echo ${SOURCEFILE%/*} /usr/local/src $ echo ${SOURCEFILE%/*} 36

37 String operators # Removes minimal matching prefixes ## Removes maximal matching prefixes % Removes minimal matching suffixes % Removes maximal matching suffixes 37

38 38

39 Control statements The three most common types of control statements: conditionals: if/then/else, case,... loop statements: while, for, until, do,... branch statements: subroutine calls (good programming practice), goto (usage not recommended). 39

40 Conditionals Conditionals are used to test something. In Java or C, they test whether a Boolean variable is true or false. In a Bourne shell script, the only thing you can test is whether or not a command is “successful”. 40

41 Conditionals Every well behaved command returns back a return code. 0 if it was successful Non-zero if it was unsuccessful (actually 1..255) This is different from C. 41

42 The if command Simple form: if decision_command then command_set fi 42

43 Example #! /bin/bash if grep unix myfile >/dev/null then echo "It's 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 43

44 The test command Use for checking validity. Three kinds: Check on files. Check on strings. Check on integers 44

45 Notes on test Testing on files. test -e file: does file exist? test –f file: does file exist and is not a directory? test -d file: does file exist and is a directory? test –x file: does file exist and is executable? test –r file: does file exist and is readable ? test –w file: does file exist and is writable ? 45

46 Notes on test Testing on files. test –s file: does file exist and is longer than 0 bytes? test file1 -nt file2: does if file1 is newer than (according to modification time) file2? test file1 -ot file2: does file1 is older than file2 46

47 Notes on test Testing on strings. test –z string: is string of length 0 (is empty)? test –n string: is string is not empty? test string1 = string2: does string1 equal string2? test string1 != string2: not equal? 47

48 Example #! /bin/bash if test -z $REMOTEHOST then : else DISPLAY="$REMOTEHOST“ echo $DISPLAY fi NOTE: This example tests to see if the value of REMOTEHOST is a string of length > 0 or not, and then sets the DISPLAY to the appropriate value. 48

49 Notes on test Testing on integers. test int1 –eq int2: is int1 equal to int2 ? test int1 –ne int2: is int1 not equal to int2 ? test int1 –lt int2: is int1 less than to int2 ? test int1 –gt int2: is int1 greater than to int2 ? test int1 –le int2: is int1 less than or equal to int2 ? test int1 –ge int2: is int1 greater than or equal to int2 ? 49

50 Example #!/bin/bash smallest=10000 for i in 5 8 19 8 7 3 do if test $i -lt $smallest then smallest=$i fi done echo $smallest NOTE: This program calculates the smallest among the numbers 5, 8, 19, 8, 3. 50

51 Notes on test The test command has an alias ‘ [] ’. Each bracket must be surrounded by spaces #!/bin/bash smallest=10000 for i in 5 8 19 8 7 3 do if [ $i -lt $smallest ] then smallest=$i fi done echo $smallest 51

52 Logical Evaluation (Conditional Expressions) The syntax of a conditional expression is [[ expression ]] where expression is a Boolean (logical) expression. You must precede a variable name with a dollar sign ($) within expression. if [[ $age>30 && $age<60 ]]; then AND and OR test : uses –a as a Boolean AND operator, and –o as OR operator [[ expression ]] : uses && as a Boolean AND operator, and || as OR operator 52

53 The elif command Simple form: if decision_command then command_set elif decision_command then command_set … else command_set fi 53

54 Using elif with if #! /bin/bash if grep "UNIX" myfile >/dev/null then echo UNIX occurs in myfile elif grep “DOS” myfile > /dev/null then echo DOS appears in myfile not UNIX else echo nobody is here in myfile fi 54

55 for loops for loops allow the repetition of a command for a specific set of values. Syntax: for var in value1 value2... do command_set done command_set is executed with each value of var (value1, value2,...) in sequence 55

56 for loops Other syntax: for var in {START..END} do command_set done for var in {START..END..INCREMENT} do command_set done for (( expr1; expr2; expr3 )) do command_set done 56

57 Notes on for Example: Listing all files in a directory. for i in * do if [ -d "$i" ] then echo "$i" fi done NOTE: * is a wild card that stands for all files in the current directory, and for will go through each value in *, which is all the files and $i has the filename. 57

58 Notes on for Examples #!/bin/bash for i in {1..5} do echo "Welcome $i times" done #!/bin/bash for i in {0..10..2} do echo "Welcome $i times" done #!/bin/bash for (( c=1; c<=5; c++ )) do echo "Welcome $c times" done #!/bin/bash for (( ; ; )) do echo "infinite loops [ hit CTRL+C to stop]" done 58

59 Example – count executables in directory #!/bin/bash count=0 for i in *; do if test –x $i then count=`expr $count + 1` fi done echo Total of $count files executable NOTE: expr $count + 1 serves the purpose of count++ 59

60 While loop Syntax while test-command do commands done 60

61 While loop #! /bin/bash i=1 sum=0 while [ $i -le 100 ] do sum=`expr $sum + $i` i=`expr $i + 1` done echo The sum is $sum. NOTE: The value of i is tested in the while to see if it is less than or equal to 100. Example 61

62 until Syntax number=0 until test-command; do commands done 62

63 until Example #!/bin/bash number=0 until [ $number -ge 10 ]; do echo "Number = $number" number=$((number + 1)) done 63

64 case Syntax case word in patterns ) commands ;; pattern ) command esac 64

65 case Example #!/bin/bash echo -n "Type a digit or a letter > " read character case $character in [a-z] | [A-Z] ) echo "You typed the letter $character" ;; [0-9] ) echo "You typed the digit $character" ;; * ) echo "You did not type a letter or a digit" esac 65

66 break Syntax break or break n 66

67 break Example #!/bin/bash a=0 while [ $a -lt 10 ] do echo $a if [ $a -eq 5 ] then break fi a=`expr $a + 1` done 67

68 break Example #!/bin/bash for var1 in 1 2 3 do for var2 in 0 5 do if [ $var1 -eq 2 -a $var2 -eq 0 ] then break 2 else echo "$var1 $var2" fi done 68

69 continue Syntax continue Or continue n 69

70 continue Example #!/bin/bash NUMS="1 2 3 4 5 6 7" for NUM in $NUMS do Q=`expr $NUM % 2` if [ $Q -eq 0 ] then echo "Number is an even number!!" continue fi echo "Found odd number" done 70

71 71

72 Using colon in shell scripts Sometimes, we do not want a statement to do anything. In that case, use a colon ‘ : ’ if grep UNIX myfile > /dev/null then : fi Does not do anything when UNIX is found in myfile. 72

73 Using semicolon in shell scripts Because a semicolon (;) ends a command just as a NEWLINE does, you can place then on the same line as if by preceding it with a semicolon Example if test-command ; then commands else commands fi 73

74 Debugging Shell Scripts You can use the shell’s –x option to help debug a script. This option causes the shell to display each command before it runs the command Example Bash –x./if2 74

75 Enable shell options You can set the an option of the shell running the script by running the set command You can set the an option of the shell running the script by putting the set command at the top of the script: Example : set –x : turn debugging on set +x : turn debugging off 75

76 Shell parameters  Shell parameters HOME PATH SHELL $0 : name of calling program $n : n’th argument $*: total argument $@: total argument $# : number of argument $? : Exit status $$ : process ID of current command 76


Download ppt "1. 2 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a."

Similar presentations


Ads by Google