Sie sind auf Seite 1von 9

Shell Scripting 101

This presentation will attempt to describe the


conditionals if, for, while, and case, along with
commonly used commands read, expr, test, and
set -x

This can be a place to just check your syntax.


if
#!/bin/sh
#
# The "if" conditional compares two values and takes action if true
#
# There are also many options, the most common of which are listed
# use man test to see the entire list
# -f File (true if File exists)
# -s File (true if File has size greater than zero)
# -d Directory (true if Directory exists)
# -n String (true if String is not empty)
# -z String (true if String is empty)
#
# Define Variable as the first parameter passed into to this script
Variable=$1
#
if [ "$Variable" = "true" ]; then
echo "Variable is true ($Variable)"
elif [ "$Variable" = "false" ]; then
echo "Variable is false ($Variable)"
else
echo "Variable $Variable is not true"
fi
for
#!/bin/sh
#
# The "for" conditional takes a space separated list of
# elements to operate on and assigns them to the Variable
# supplied in the second field, in this case Element
#
for Element in one two three ; do
echo $Element
done

The output looks like this:

# ./for.sh
one
two
three
while
#!/bin/sh
#
# An example of using while to wait for a file to exist
#
SpecialFile="${1:-/tmp/BeginAutoTask}"
[ -f $SpecialFile ] && FileCheck=true
echo "Watching for $SpecialFile to show up"
while [ "$FileCheck" != "true" ]; do
[ -f $SpecialFile ] && FileCheck=true
echo -e ".\c"
sleep 10
done
echo "$SpecialFile Exists now, running the one time process.."
rm $SpecialFile
#
# Insert Automated Process Here
#Eg:
# Suspend
# Update
# Resume
#
echo
ExitCode=$?
if [ $ExitCode -ne 0 ]; then
echo "An Error may have occurred"
else
echo "The Process was Successful"
fi
case
#!/bin/sh
#
echo "This is the Server menu:
1) sc58lsigp01
2) sc58lsigp02
3) sc58lsigp03
4) sc58lsigp04
"
echo -e "Which server would you like to log into? [1] \c"
read Variable
#
case $Variable in
2) Server=sc58lsigp02;;
3) Server=sc58lsigp03;;
4) Server=sc58lsigp04;;
1|*) Server=sc58lsigp01;; # Default Last
esac
#
echo "Logging into $Server"
#
ssh $Server
read
#!/bin/sh
#
# The read command is used to assign user responses to variables
#
echo "Waiting for your answer..."
read Response
echo
echo "Thank you, you entered $Response"
#
echo -e "\nWould you like to interact some more? [y] \c"
read Answer
if [ "$Answer" = "" -o "$Answer" = "y" ]; then
# Put more functionality here
echo "I am not programmed to continue with this dialogue yet."
#
else
echo "I understand, nothing personal. Have a good day!"
fi
expr
#!/bin/sh
#
# The expr utility can be used to use arithmetic equations on variables which are
numbers.
#
Base=${1:-10}
Add=${2:-5}
Total=`expr $Base + $Add`
#
echo "$Base + $Add = $Total"

So execution of this script provides the following results:

# ./expr.sh
10 + 5 = 15
# ./expr.sh 20
20 + 5 = 25
# ./expr.sh 20 32
20 + 32 = 52
test and set -x
#!/bin/sh
#
# test is a command to determine if a value is successful
# in shell, test is actually the left square bracket [
#
# set -x can be used to display code execution
# and can be turned back off with set +x
set -x
#
parmOne=${1:-true}
parmTwo=${2:-true}
#
[ "$parmOne" = "$parmTwo" ] && echo "$parmOne is equal to $parmTwo"
#
set +x
#
test "$parmOne" != "$parmTwo" && echo "$parmOne is not equal to $parmTwo"

So execution of this script looks like this:

# ./test.sh true false


+ parmOne=true
+ parmTwo=false
+ '[' true = false ']'
+ set +x
true is not equal to false
Subroutines
#!/bin/sh
#
Output=${1:-output}
#
# Define Subroutine
subRoutine(){
echo $Output
}
#
# Call Subroutine
subRoutine

Das könnte Ihnen auch gefallen