Shell Programming Flashcards
Shell Programming Topics
- Variables and Environment
- Parameters and Quoting
- Grouping
- Exit Values
- Testing
- Repeating
- Miscellaneous
Shell Programming:
Variables and
Environment
- Shell supports local variables
- local to a script
- Assigning:
- name = value
- Use:
- Reference using ‘$’
- echo $name
- Be careful of blanks(unassigned variables)
- You can export a variable to make it available to any other scripts invoked by the original script:
- %export MODE = “smart”
- %script2
- This exports allows script2 to make use of the MODE variable
Variables Inherited
from the Environment
File Descriptors:
- stdin
- stdout
- stderr
Locations:
- current working directory
Variables:
- any exported variables
Shell Scripts:
Parameters
Parameters are passed to a script like command line arguments:
$myScript arg1 arg2 “some string”
The parameters are referenced in the script as numerical variables:
$@ - set of parameters
$1 - first argument
$2 - second argument, etc
Dollar signs trigger parameter substitution
Quotes or backslashes suppress parameter substitution
Shell Scripts:
Command Substitution
Dollar signs with parenthesis run a command and substitute the stdout
form: $( command )
example:
“ab$(echo hello world)yz”
Will be seen as:
“abhello worldyz”
Text can also use older backquote system
Shell Scripts:
Grouping:
Combine Commands on
one line
Simple: separate with a semicolon
Example:
thing = “blah blah”; echo $thing
This assigns the variable thing,
then echos it
Shell Scripts:
Grouping:
Establish a New Context
Use parenthesis to establish a new context.
example:
(cd /home/sarah; pwd; echo “done”)
This changes the working directory, but only within the new context.
Commands only affect the current context.
Only inner context changed working directory.
Shell Scripts:
Exit
Use command:
exit <status></status>
Status returns 0 on success (True)
nonzero on failure (False)
Shell Scripts:
Comments
Use the #
example:
echo $myVar #stupid comment
Shell Scripts:
Testing:
If Statements
do stuff
Nothing around condition, must end with “fi”,
explicitly state “then” and “else”
Form:
if condition
then
else
fi
Shell Scripts:
Testing:
Bracket Program
The Bracket Program provides some Testing Functions
- Surround test with brackets
- [-d file] -> true if file exists and is directory
- [-f file] -> true if file exists and is not a directory
- [string1 = string2] -> Strings are identical
- [string1 != string2] -> Strings are different
- [file1 -nt file2] -> file1 is newer than file2
- other flags:
- -e
- -r
- -w
Shell Scripts:
Repeating:
For Loop
Form:
for x in <set></set>
do
….
done
Shell Scripts:
Arithmetic:
Evaluate an expression
Use the “let” command prior to doing math:
let n=1
let n=($n + 2)*3; echo $n
let ++n ; echo $n
Output:
9
10
Shell Scripts:
Including a file
Use the “.” command
This includes a file into a script as if it were typed there.
.external_script
Shell Scripts:
While-Do Loop Form
perform actions
while [condition]
do
done