Presentation is loading. Please wait.

Presentation is loading. Please wait.

©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.

Similar presentations


Presentation on theme: "©Colin Jamison 2004 Shell scripting in Linux Colin Jamison."— Presentation transcript:

1 ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

2 ©Colin Jamison 2004 Shells A shell is an interface between the user and the Unix system, which allows the user to enter commands which the operating system executes There are a variety of shells available e.g. sh, csh, ksh, bash, etc. We shall use bash

3 ©Colin Jamison 2004 Interactive Shell Use The shell can be used to directly input and run shell programming code It automatically performs wildcard expansions [set] allows any number of single characters to be checked [^set] negates the set of characters to be checked Brace expansion {} possible in bash

4 ©Colin Jamison 2004 Creating a Script First line of the script starts with #!/bin/sh On all other lines, comments start with # and continue to the end of the line Environment variables for the shell script are the same as those for the user who runs the script

5 ©Colin Jamison 2004 Run a Script To make the script executable chmod +x scriptname {Remove write permissions for other users} To run script type./scriptname

6 ©Colin Jamison 2004 Syntax - Variables Variables are created when we first use them By default all variables are stored as strings To get at the contents of a variable precede it by $ Quotes: Variable name in double quotes - replaces variable with its value Variable name in single quotes - prints variable name literally

7 ©Colin Jamison 2004 Variables - Example #!/bin/sh var=“Hello” echo $var echo “$var” echo ‘$var’ echo \$var exit 0 Output: Hello $var

8 ©Colin Jamison 2004 Environment Variables $HOME - users home directory $PATH - colon separated list of directories $0- shell script name $#- number of parameters passed $$- PID of the shell script $IFS - Input field separator (usually whitespace)

9 ©Colin Jamison 2004 Parameter Variables $1, $2,... - parameters passed to the script $* - list of all parameters separated by IFS $@ - list of all parameters stored as separate fields

10 ©Colin Jamison 2004 Syntax - Booleans If [ -f myC.c ] then … fi Note the space between each part of the conditional statement - this is required!

11 ©Colin Jamison 2004 String Comparison “$string1” = “$string2” True if equal “$string1” != “$string2” True if not equal -n “$string” True if string is not null -z “$string” True if string is null

12 ©Colin Jamison 2004 Arithmetic Comparison expression1 operator expression2 operator -eq equal to operator -ne not equal operator -gt greater than operator -ge greater than or equal to operator -lt less than operator -le less than or equal to ! expression True if expression false and vice versa

13 ©Colin Jamison 2004 File Conditionals -d fileTrue if file a directory -e fileTrue if the file exists -f fileTrue if the file is a regular file -r fileTrue if the file is readable -s fileTrue if the file size is non-zero -w fileTrue if the file is writeable -x fileTrue if the file is executable

14 ©Colin Jamison 2004 Syntax - Program Control (1) if [ condition ] then statements else statements fi elif allows more conditional tests (instead of else) for variable in values do statements done

15 ©Colin Jamison 2004 Syntax - Program Control (2) while [ condition ] do statements done until [ condition ] do statements done

16 ©Colin Jamison 2004 Syntax - Program Control (3) case variable in pattern [ | pattern] …) statements;; … esac

17 ©Colin Jamison 2004 Syntax - Lists The AND list [ statement1 ] && [ statement2 ] && … Fails at the first false statement The Or list [ statement1 ] || [ statement2 ] || … Passes at the first true statement

18 ©Colin Jamison 2004 Syntax - Functions function_name () { statements } You must always define a function before invoking it Return a numerical value using the return command Declare local variable/s by using the local keyword

19 ©Colin Jamison 2004 Syntax - Built-in Commands (1) breakcan be used to break out of an enclosing for, while or until loop the : command: is an alias for true and useful for the conditional setting of variables continuemakes the enclosing for, while or until loop continue at the next iteration the. commandexecutes the command in the current shell echooutputs a string to standard output evalallows the evaluation of arguments execnormally used for replacing the current shell with a different program

20 ©Colin Jamison 2004 Syntax - Built-in Commands (2) exit ncauses the script to exit with exit code n exportmakes the variable named as its parameter available in sub-shells expruse $((…)) printfuse in preference to echo in generating formatted output returncauses a function to return setsets the parameter variables for the shell shiftmoves all the parameter variables down by one e.g. $2 becomes $1

21 ©Colin Jamison 2004 Syntax - Built-in Commands (3) trapused to specify the action to take on the receipt of signals unsetremoves variables or functions from the environment

22 ©Colin Jamison 2004 Command Execution Arithmetic expansion x=$(($y+1)) Parameter expansion e.g. variable called counter - $file_number_${counter} other parameter expansions/substitutions are possible e.g. ${param:-default} ${#param} ${param#word} ${param##word} ${param%word} ${param%word}

23 ©Colin Jamison 2004 Here Documents A special way of passing input to a command from a shell script command <<unique_descriptor statements unique_descriptor

24 ©Colin Jamison 2004 Questions ?


Download ppt "©Colin Jamison 2004 Shell scripting in Linux Colin Jamison."

Similar presentations


Ads by Google