linux 迴圈控制語句

polestar123發表於2009-02-10

while
syntax
while
test-commands
do
commands
done
description
execute test-command(usually a test commnad) and if the exit status is zero, perform commands, repeat. opposite until.
until
syntax
until
test-commands
do
commands
done
description
execute test-commands(usually a test command) and if the exit status is nonzero(that is, the test fails), perform commands; repeat. opposite of while.
巢狀的while loop
while cmd1
do
commands
while cmd2
do
commands
done
commands
done

example:
#using while loop
x=0
while [ $x -lt 10 ]
do
echo $x
x=$(($x+1))
done
#using until loop
until [ $x -lt 0 ]
do
echo $x
x=$(($x-1))
done
#using multiple while loop
x=0
while [ $x -lt 10 ]; do
y="$x"
while [ $y -ge 0 ]; do
printf "$y"
y=$(($y-1))
done
echo x=$(($x+1))
done
#while associate with stdin redirection(while迴圈和輸入重定向,從檔案中讀出一行)
#!/bin/bash
#calculate the line counts of a file(計算一個檔案的行數)
if [ -f "$1" ]
then
i=0
while read LINE
do
i=$(($i+1))
done<"$1"
echo $i
fi
#invoking like this: source ./script_filename file(scan file)

for
syntax
for x in list
do
commands
done
description
assign each word in list to x in turn and execute commands. if list is omitted, it is assumed that positional parameters from the commands line, which are storecd in $@, are to be used.
example
for i in 0 1 2 3 4 5 6 7 8 9
do
echo $i
done

example2:
#!/bin/bash
#copy all files of $HOME/files/ to $HOME/c/(複製當前主目錄下的目錄為files裡的所有檔案到主目錄下的目錄c中)
for j in $HOME/files/*
do
cp $HOME/files/* $HOME/c
done

select
syntax
select variable in list
do
case variable in
var1) commands;;
var2) commands;;
.....
varN) commands
esac
done
example:
#!/bin/bash
select digit in 1 2 3 4 5
do
case $digit in
1|2) printf "this is the digit 1 and 2 n";;
3) printf "this is the digit 3n";;
4) printf "this is the digit 4n";;
*) printf "this the other digitn";break
esac
done

change the value of ps3 to change the prompter of select loop:
$PS3="please make a selection => "; export PS3

break
syntax
break [n]
description
Exit from the innermost(most deeply nested) for, whie, or until or from the n innermost levels of the loop

example:
for i in 1 2 3 4 5
do
mkdir -p /mnt/backup/docs/ch0${i}
if [ $? -eq 0 ]
then
for j in doc c h m pl sh
do
cp $HOME/docs/ch0${i}/*.${j} /mnt/backup/docs/cho${i}
if [ $? -ne 0 ]; then break 2;
fi
else
printf "could not make backup directorys.n"
fi
done

fi

done


continue
syntax
continue [n]
skip remaining commands in a for, while, or until loop, resuming whith the next iteration of the loop(or skipping n loops)
example
for FILE in $FILES
do
if [ ! -f $FILE ]
echo "ERROR, the $FILE is not a file"
continue
fi
#process file
done

exit
syntax
exit [n]
Exit a shell script with status n. The value for n can be 0(success) or nonzero(failure). if n is got given, the exit status is that of the most recent command.

example:
if ! test -f somefile
then
echo "somefile is not a file"
exit 1
fi

read
syntax
read [option] variable1 [variable2 .....]
Read one line of standard input, and assign each word to the corresponding variablewith all remain words assinged to the last variable.

example
echo "Enter last-name, height, and weight"
read lastname everythigelse
echo $lastname
echo $everythingelse
The name entered is placed in variable $lastname; all of the other values, includeing the spaces between them, are placed in $everythingelse.

return
syntax
return [n]
This command is used inside a function to exit the function with status n. if n is omitted, the exit status of the previously executed command is returned.

shift
shift [n]
shift positional parameters down n elements, if n is omitted, the default is 1, so $2 become $1, $3 become $2, and so on.(省去前n個元素,如果n 省略,就是省去第一個引數)

:
#!/bin/bash
if [ $# -lt 2 ]; then
echo "the options less than 2"
exit 1
fi
shift
for i in $@
do
case $i in
witi) echo "this is file1";;
apple) echo "this is a apple";;
orange) echo "$2 this is the second item,the '$#' is $# and the '$*' is $* and
the $@"
;;
*) echo "this is other";;
esac
done
#the script terminate
invoking it: $source ./file2 kiwi apple orange banana

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70612/viewspace-1017151/,如需轉載,請註明出處,否則將追究法律責任。

相關文章