Shell Scripting: Intermediate Systems Administration Decal Lecture #6 Joshua Kwan
Shell Scripting: Intermediate Systems Administration Decal Lecture #6 Joshua Kwan
• Assignment: FOO=“Test 1 2 3”
• Reference: echo $FOO or echo “$FOO”
(What’s the difference?)
• Want to set a variable to the output of a
command? Input substitution!
FOO=$(ls pictures)
Special Shell Variables
• $1, $2, $3.. - arguments passed in on
command line.
• $$ - your process ID
• $! - process ID of last program started w/ ‘&’
Functions
• When you make a shell script, lines of code
are executed top-to-bottom
• If you make functions, they won’t be run
though, just declared.You can use them as if
they were separate programs.
• Learn by example! We know enough to
write a simple program now.
Example 1
#!/bin/sh
confuciusprint() {
echo "Confucius say: \"$@\""
}
confuciusprint "$@"
confuciusprint "$1"
Control structures
• For loops set a variable based on the contets of a
list (like python, unlike C):
for x in $(seq 1 9); do touch $x;
done
FILE="$1"
if [ ! -f "$FILE" ]; then
exit 1
fi