User:Spoonbender/Temp/Scripting.doc
Contents
Writing Basic Shell Programs
- A text file containing UNIX commands
- A text with read and execute permissions
- A program executed/interpreted by the shell
- A tool for automating UNIX tasks
- A program that doesn’t need to be compiled
Step 1. Create a text file using vi
Step 2. Put commands inside the text file
Step 3. Save and exit from vi
Step 4. Add an execute permission to the text file
Step 5. Run the script using either relative or absolute filename
- #!/usr/bin/shInterpret using default shell
- #!/usr/bin/cshInterpret using C shell
- #!/usr/bin/kshInterpret using Korn shell
Variables
Variable assignment:
variable_name=value
or
variable_name=“value string”
Using variable values:
$variable_name
To make a variable an environment variable, we use the command:
export variable_name
Command Line Positional Parameters
The arguments on the command line are referenced within the shell program through special variables that are defined relative to an argument’s position in the command line called positional parameters
$ prog_name arg1 arg2 … argX
Parameter | Description | Variable Substitution |
* | All positional parameters | $* |
@ | All positional parameters | $@ |
# | Number of positional parameters | $# |
Read and Shift Commands
The read command is used to gather information typed at the terminal during program execution
read variable_name
The shift command will reassign the command-line arguments to the positional parameters, allowing you to increment through the command-line arguments
shift [n]
Using Return Codes
All UNIX commands will generate a return code upon completion of the command
echo $?
0 The previous command worked successfully (TRUE)
1 The previous command worked but was not successful (FALSE)
2 The previous command did not work. (FALSE). Probable causes could be invalid command invocation (syntax) or incorrect file/directory name
126 The previous command failed due to inappropriate file/directory permission settings (the message: “permission denied” is usually presented to stderr)
127 The command could not be found and executed due to: incorrect pathname or incorrect command name (the message: “not found” is usually presented to stderr)
129-160 The command exited due to the receipt of a signal
Testing Parameters
The test command is used to evaluate expressions and generate a return code
test expression or [ expression ]
The test command can evaluate the condition of
- Integers
- Strings
- Files
Numeric Tests
[ exp1 –eq exp2 ]
[ exp1 –ne exp2 ]
[ exp1 –lt exp2 ]
[ exp1 –gt exp2 ]
[ exp1 –le exp2 ]
[ exp1 –ge exp2 ]
String Tests
[ string1 = string2 ]
[ string1 != string2 ]
[ string1 < string2 ]
[ string1 > string2 ]
[ -n string ] String is non-zero in length
[ -z string ]String is zero in length
File Tests
-e fileTrue, if file exists
-d dirTrue, if dir is a directory
-f fileTrue, if file exists and is an ordinary file
-s fileTrue, if file exists and has a size greater than zero
-r fileTrue, if file exists and is readable
-x fileTrue, if file exists and is executable
-w fileTrue, if file exists and is writable
Other Operators
[ exp1 –o exp2 ]OR
[ exp1 –a exp2 ]AND
test ! –d fileNOT
[ \(exp1 –a exp2\) –o exp2 ] GROUPING
The IF and CASE Commands
The if … then … fi Statement
if expression
then
...
commands
...
fi
The else Statement
if expression
then
...
commands executed if condition is True
else
...
commands executed if condition is False
fi
Compound if Statements
if condition_1
then
commands executed if condition_1 is True
elif condition_2
then
commands executed if condition_2 is True
elif condition_3
then
commands executed if condition_3 is True
else
commands executed if condition1,2&3 are False
fi
The case Statement
case value in
pattern_1)commands to execute ;;
pattern_2)commands to execute ;;
pattern_n)commands to execute ;;
esac
Shell Programming Loops
Arithmetic Evaluation
The let command enables the shell scripts to use arithmetic expressions
let expression or (( expression ))
The WHILE Command - Repeat the loop while the condition is true
while condition
do
commands
done
The UNTIL Command - Repeat the loop until the condition is true
until condition
do
commands
done
The FOR Command - For each item in list, repeat the loop, assigning var to the next item in list until the list is exhausted
for var in list
do
commands
done
The break, continue, and exit
break [n] - Terminates the iteration of the loop and skips to the next command after [the nth] done
continue [n] Stops the current iteration of the loop and skips to the beginning of the next iteration [of the nth] enclosing loop
exit [n]Stops the execution of the shell program, and sets the return code to n