linux 迴圈控制語句
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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70612/viewspace-1017151/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 流程控制、 迴圈語句
- JavaScript 流程控制語句詳解:if語句、switch語句、while迴圈、for迴圈等JavaScriptWhile
- php中有哪些迴圈控制語句PHP
- 【分支流程控制語句、迴圈流程控制語句】的學習
- 分支語句和迴圈語句
- Ruby迴圈語句
- TypeScript 迴圈語句TypeScript
- MySQL迴圈語句MySql
- JavaScript for 迴圈語句JavaScript
- 2-4 Java流程控制——迴圈語句Java
- Python的迴圈語句Python
- 7-迴圈語句
- 七 while迴圈語句While
- JavaScript跳出for迴圈語句JavaScript
- 第 7 節:流程控制-迴圈練習-跳出語句
- Python-條件語句和迴圈語句Python
- Python 迴圈語句的使用Python
- MyBatis xml foreach迴圈語句MyBatisXML
- 【Python基礎】for迴圈語句Python
- 『無為則無心』Python基礎 — 15、Python流程控制語句(for迴圈語句)Python
- python04: while迴圈語句 break continue for in 迴圈PythonWhile
- c語言中,while(1)語句使用break語句跳出迴圈C語言While
- Python之判斷迴圈語句Python
- 分支、迴圈語句動態展示
- Java簡單迴圈語句案例Java
- Python基礎-While迴圈語句PythonWhile
- Python條件語句與迴圈Python
- [譯] part 9: golang 迴圈語句Golang
- Verilog HDL迴圈語句簡介
- 初學Python(3)迴圈語句Python
- Java迴圈語句以及break,continue語句,IDEA安裝JavaIdea
- Java 可以採用什麼語句跳出迴圈語句Java
- Java基礎 迴圈語句 for while do.....while語句JavaWhile
- Java語言程式設計—迴圈語句Java程式設計
- Python學習筆記3(條件語句+迴圈語句)Python筆記
- 用Java中for迴圈語句列印菱形Java
- python迴圈語句判斷的使用Python
- SqlServer中迴圈和條件語句SQLServer
- java常見語句之迴圈體Java