©colin jamison 2004 shell scripting in linux colin jamison

24
© Colin Jamison 004 Shell scripting in Linux Colin Jamison

Upload: myles-ellis

Post on 13-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Shell scripting in Linux

Colin Jamison

Page 2: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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

Page 3: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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

Page 4: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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

Page 5: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Run a Script

• To make the script executable chmod +x scriptname

• {Remove write permissions for other users}

• To run script type ./scriptname

Page 6: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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

Page 7: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Variables - Example

#!/bin/shvar=“Hello”echo $varecho “$var”echo ‘$var’echo \$varexit 0

Output:HelloHello$var$var

Page 8: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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)

Page 9: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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

Page 10: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Syntax - Booleans

If [ -f myC.c ]then…fi

• Note the space between each part of the conditional statement - this is required!

Page 11: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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

Page 12: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Arithmetic Comparison

expression1 operator expression2operator -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

Page 13: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

File Conditionals

-d file True if file a directory

-e file True if the file exists

-f file True if the file is a regular file

-r file True if the file is readable

-s file True if the file size is non-zero

-w file True if the file is writeable

-x file True if the file is executable

Page 14: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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

Page 15: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Syntax - Program Control (2)

while [ condition ] do

statements

done

until [ condition ]

do

statements

done

Page 16: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Syntax - Program Control (3)

case variable in

pattern [ | pattern] …) statements;;

pattern [ | pattern] …) statements;;

esac

Page 17: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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

Page 18: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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

Page 19: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Syntax - Built-in Commands (1)

• break can 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

• continue makes the enclosing for, while or until loop continue at the next iteration

• the . commandexecutes the command in the current shell

• echo outputs a string to standard output• eval allows the evaluation of arguments• exec normally used for replacing the current

shell with a different program

Page 20: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Syntax - Built-in Commands (2)

• exit n causes the script to exit with exit code n

• export makes the variable named as its parameter available in sub-shells

• expr use $((…))

• printf use in preference to echo in generating formatted output

• return causes a function to return

• set sets the parameter variables for the shell

• shift moves all the parameter variables down by one e.g. $2 becomes $1

Page 21: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Syntax - Built-in Commands (3)

• trap used to specify the action to take on the receipt of signals

• unset removes variables or functions from the environment

Page 22: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

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}

Page 23: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Here Documents

• A special way of passing input to a command from a shell script

command <<unique_descriptor

statements

unique_descriptor

Page 24: ©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Col

in J

amis

on

2004

Questions ?