Posts Tagged ‘statement’

for statement

Posted: 09/08/2011 in Shell Scripting
Tags: , , ,

syntax:
for (name) in (words)
do
(loop-body-statements)
done

e.g.:

for planet in Mercury Venus Earth Mars \
Jupiter Saturn Uranus Neptune Pluto
do
echo $planet
done

for i in *.txt
do
echo $i
grep ’lost treasure’ $i
done

i=0
for parameter
do
let ’i = i + 1’
echo "parameter $i is $parameter"
done

while statement

Posted: 09/08/2011 in Shell Scripting
Tags: , , ,

syntax:
while (test-commands)
do
(loop-body-statements)
done

e.g.:

i=0
while [ "$i" -lt 10 ]
do
echo -n "$i " # -n
let "i = i + 1" # i=$(expr $i + 1) 

if-statement

Posted: 09/08/2011 in Shell Scripting
Tags: , , ,

syntax:
if (test-commands)
then
(statements-if-test-commands-1-true)
elif (test-commands-2)
then
(statements-if-test-commands-2-true)
else
(statements-if-all-test-commands-false)
fi

e.g.:

if grep $1 /etc/passwd > /dev/null 2>&1
then
echo $1 has a local account here
else
echo $1 has no local account here
fi