©Col
in J
amis
on
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
©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
©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
©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
©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
©Col
in J
amis
on
2004
Variables - Example
#!/bin/shvar=“Hello”echo $varecho “$var”echo ‘$var’echo \$varexit 0
Output:HelloHello$var$var
©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)
©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
©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!
©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
©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
©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
©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
©Col
in J
amis
on
2004
Syntax - Program Control (2)
while [ condition ] do
statements
done
until [ condition ]
do
statements
done
©Col
in J
amis
on
2004
Syntax - Program Control (3)
case variable in
pattern [ | pattern] …) statements;;
pattern [ | pattern] …) statements;;
…
esac
©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
©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
©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
©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
©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
©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}
©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
©Col
in J
amis
on
2004
Questions ?
Top Related