Linux Shell——循環與結構化命令

for循環

for variable in (list)
do
	command
done	
#!/bin/bash

for variable in {1..5}
do
	echo "Hello,Welcome $variable times"
done
#!/bin/bash
#步長爲2
for variable in {1..5..2}
do
	echo "Hello,Welcome $variable times"
done

不帶列表的for循環

for variable
do
	command
	command
done

類C風格的for循環

for((expr1;expr2;expr3))
do
	command
	command
done	

while循環

while expression
do
	command
	command
done

計數器控制的while循環

counter=1
while expression
do
	command

done

結束標記控制的while循環

read variable
while [[ "$variable" != sentinel ]]
do
	read variable
done

標誌控制的while循環

signal=0

while (( signal != 1 ))
do
	if expression
	then
		signal=1
	fi
done

命令行控制的while循環

while [[ "$*" != "" ]]
do
	echo "$1"
	shift
done

until循環

在執行while循環時,只要expression的退出狀態爲0,將一直執行循環體,until命令和while命令類似,但區別是util循環中的expression的退出狀態不爲0,循環體將一直執行下去,直到退出狀態爲0。

循環控制符

break

continue

select結構

select variable in {list}
do
	command
	break
done
#!/bin/bash

echo "What is your favorite color?"

select color in "red" "blue" "green"
do
	break
done


echo "You have selected $color"

 ./select_exam1.sh 
What is your favorite color?
1) red
2) blue
3) green
#? 1
You have selected red


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章