Shell Programming Flashcards
Sequential Logic
to execute commands in the order in which they appear
Decision Logic
to execute commands only if a certain condition is satisfied
Looping Logic
to repeat a series of commands for given number of times
Case Logic
to replace “if then/else if/else” statements when making numerous comparisons
Positional Parameters
special variables used when shell scrit or shell function is called with argument parameters
$#
number of input parameters
$0
Name of the script
$1, $2, $3, …. , $9
first, second, third, and 9th argument parameter
$@
List of input parameters
$*
List of input parameters as space separated string
shift
shifts the positional parameters by one towards the beginning and drops $1 from the list. After a shift, $2 becomes $1 and so on (note if more than 9 arguments, they cannoted be directly accessed by $1 to $9 . Must use shift command
Example of Positional Variables
cat myinputs.sh #!/bin/sh echo Total number of inputs: $# echo First input: $1 echo Second input: $2
$ chmod u+x myinputs.sh $ ./myinputs.sh ONE TWO BUCKLE MY SHOE Total number of inputs: 5 First input: ONE Second input: TWO
Decision Logic: Conditional
if test-command then execute command elif test-command then execute this and execute this command else execute default command fi
What is a TEST command?
testis a built in bash commands. that returns true (0) ir false (non-zero) for a given set of arguments
Arguments structure for TEST
Argument structure is:
test item1 comparator item2
or
test option item1
• Instead of calling test, can use square brackets:
[ item1 comparator item2 ]
[ option item1 ]
you must have spaces around the brackets
test Options for File Inquiry
[ -d filename ] Test if filename is a directory
[ -f filename ] Test if filename is not a directory
[ -s filename ] Test if filename has non zero length
[ -r filename ] Test if filename is readable
[ -w filename ] Test if filename is writable
[ -x filename ] Test if filename is executable
[ -o filename ] Test if filename is owned by the user
[ -e filename ] Test if filename exists
[ -z filename ] Test if filename has zero length
All these conditions return exit status code ($?) of true (0) if satisfied and false (1) otherwise
[ -d filename ]
Test if filename is a directory
[ -f filename ]
Test if filename is not a directory
[ -s filename ]
Test if filename has non zero length
[ -r filename ]
Test if filename is readable
[ -w filename ]
Test if filename is writable
[ -x filename ]
Test if filename is executable
syntax for Combining Tests
&& represents AND
|| represents OR
Syntax: if cond1 && cond2 || cond3 … An alternative form is to use a compound statement in test using –a and –o keywords. For example: if [ cond1 –a cond2 –o cond3 … ] where cond1, cond2, cond3 are either commands returning a value or test conditions of the form: test arg1 op arg2 or [ arg1 op arg2 ]
Looping Logic
A loop is a block of code that is repeated a number of times. Either:
– A pre-determined number of times determined by a list of items
in the loop count ( for loops), or
– Until a particular condition is satisfied ( while and until
loops)
To provide flexibility to the loop constructs there are also
two statements for control:
– continue : skips to the next item in a for loop
– break : exits out of a loop