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