Lecture Ten
Introduction to Scripting
Introduction to Scripting
- a shell script is simply a collection of commands in a file, similar to a DOS bat file
- to run a script by just using the script name:
- the script must have r and x permission
- the script must be in a directory that's included in your PATH, otherwise a path must be provided
- except on the first line, everything following # on a line is a comment
- if first line is
#!pathname
(starting in the first column), specified shell is used to run the script- eg.
#!/bin/bash
or#!/bin/ksh
- ensures that script will run in correct shell regardless of which shell user is using
- if not used, script will run in the current shell type
- eg.
Variables
- user-created variable names must begin with a letter
- eg.
name=lczegel
(no spaces around = )
- eg.
- precede variable name with $ for value substitution, eg.
echo $name
- variables can be read-only, eg.
readonly course=ULI101
- existing variables can be made read-only, eg.
readonly name
readonly
by itself will list all existing read-only variables
- existing variables can be made read-only, eg.
- variables may be removed as follows:
- eg.
name=
- eg.
unset name
- read-only variables will last the life of the login session
- eg.
read will read one line from standard input and assign it to variables, for example:
==> cat read.example echo -n "Please enter your name: " read name echo "Hello $name" ==> read.example Please enter your name: Josephine Student Hello Josephine Student ==> _
$1 to $9 are first nine positional parameters, or command line arguments
==> cat arguments.example1 echo "\$1 is $1" echo "\$2 is $2" ==> arguments.example1 lion tiger bear $1 is lion $2 is tiger ==> arguments.example1 elephant $1 is elephant $2 is ==> _
can access beyond $9 by using set braces, eg. ${10} will access 10th argument
- $* represents all parameters, as a single string
- $@ represents all parameters, as separate strings
- $# contains number of parameters
shift
shifts parameters left, so that the first one (which was $1) disappears, the second one which was $2 becomes $1, etc.example of
$#
andshift
:==> cat arguments.example2 echo "\$1 is $1" echo "\$2 is $2" echo "\$# is $#" shift echo "After shift:" echo "\$1 is $1" echo "\$2 is $2" echo "\$# is $#" ==> arguments.example2 lion tiger bear $1 is lion $2 is tiger $# is 3 After shift: $1 is tiger $2 is bear $# is 2 ==> _
$$ is the PID (process ID) number of the current process - useful for naming temporary files
- $? is the exit status of the last command, 0 means successful
Environment Variables
- some common environment variables:
PS1 -
primary promptPWD -
present working directoryOLDPWD -
previous working directory, used bycd -
HOME -
absolute path to user's home directory, similar to ~HOST -
name of hostUSER -
name of current userSHELL -
current shellTERM -
terminal type being emulatedPATH -
list of directories containing executables (programs)- colon-delimited list, try
echo $PATH
- for example, to add current directory to PATH:
PATH=$PATH:.
- more than one directory may contain a particular executable
- directory list is searched left to right, first matching executable name is used
- can use
which
command to identify which executable will be used - for example,
which grep
will show which executable will be used whengrep
is entered on command line
- colon-delimited list, try
- can show all variables with
set
command using no arguments
Shell Arithmetic
expr
is used to evaluate integer expressions in the Bourne shell (also works in Bash and Korn)- spacing is inflexible, and special characters must be quoted
- eg.
expr ( 2 + 3 ) * 5
- eg.
x=expr $x + 1
let
performs arithmetic in Bash and Korn shells- can't use spaces, and $ optional in front of variables
- eg.
let x=(2+3)*5
- eg.
let x=x+1
(( expression ))
allows any spacing (works in Bash and Korn)- this is probably the easiest-to-use form of arithmetic expression
- eg.
((x = (2 + 3) * 5))
- eg.
x=$(( x + 1 ))
- eg.
echo $(((x + 6) / 3))
$[ expression ]
performs arithmetic in the Bash shell only, used same as$(( ... ))
Control Structures
if-then
if
is used to execute, or not execute, contained statements, depending on some condition[ ... ]
is same astest
command (see test criteria and examples under description of test command - page 794 in the text)to check if the right number of arguments were passed to the script:
if [ $# != 3 ] then echo "This script requires 3 arguments" >&2 exit 1 fi
or to check an exit status of the previous command:
grep -i "$1" cars if [ $? != 0 ] then echo Sorry, $1 is not in the cars file >&2 exit 1 fi
or to check if a filename passed as an argument is an existing directory:
if [ ! -d $1 ] then echo The first argument $1 is not a directory >&2 exit 1 fi
other
test
options:-f -
check if file is an ordinary file-e -
check if file exists, either directory or ordinary file-s -
check if file exists, with file size greater than 0-r -
check if file exists, and you have read permission-w -
check if file exists, and you have write permission-x -
check if file exists, and you have execute permission-n -
check if string has non-zero length-z -
check if string has zero length- to compare numbers, can use
-lt -le -gt -ge -eq -ne
- eg.
if [ $salary -gt 45000 ]
- eg.
if test $salary -gt 45000
- eg.
for-in
for
is used to execute statements repeatedly, for a specifed number of repetitions- a loop variable takes the values of a specified list, one at a time
for example, to process a list of strings:
for animal in lion tiger bear do echo $animal done
another example, to process a list of strings:
for count in 3 2 1 'BLAST OFF!!!' do sleep 1 echo $count done