/******************************************************************
作者:edwin(▓男兒〆無淚)
日期:2010.7.27
注: 轉載請保留此信息
更多內容,請訪問我的博客:blog.csdn.net/windxfxx8
******************************************************************/
rhel下shell腳本的語法總結
if
條件判斷控制語句:
if 條件
then 動作
elif 條件
then 動作
else
動作
fi
註釋:
•if,當條件爲真時,執行then後的動作。elif在if判斷爲假時才做判斷,else在if與elif都爲假時執行。
•Example:
# ! /bin/sh
read –p “Enter a password “ pwd_entered
if [ “$pwd_entered” = “password” ]
then
echo Password is correct
else
echo Password is incorrect
fi
case
條件判斷控制語句:
case 表達式 in
匹配值 1)
動作;;
匹配值 2)
動作;;
* )
動作;;
esac
case
條件判斷控制語句:
case 表達式 in
匹配值 1)
動作;;
匹配值 2)
動作;;
* )
動作;;
esac
•case,根據表達式的值(表達式中通常會包含變量),尋找匹配項執行動作。可以用於取代一組if語句。
•Example:
# ! /bin/sh
read –p “Enter a password “ pwd_entered
case $pwd_entered in
password1 )
echo You entered password1;;
password2 )
echo You entered password2;;
esac
select
循環控制語句
select 變量 in 數組
do
動作
done
select
循環控制語句
select 變量 in 數組
do
動作
done
注意:
•select,無限循環,直到遇到退出循環的命令(exit、break)或鍵盤送來的ctrl+c才退出循環。每次循環都要求用戶從數組中爲變量選取一個值。
for
循環控制語句
for 變量 in 數組
do
動作
done
註釋:
•for,變量自動去數組中取值,每取一次值執行一次循環。
•Example:
# ! /bin/sh
read –p “Enter several words “ words_entered
clear
echo The words you entered are:
for word in $word_entered
do
echo $word
done
while
循環控制語句
while 條件
do
動作
done
註釋:
while
循環控制語句
while 條件
do
動作
done
註釋:
•while,當條件爲真時,執行其下的循環。
•Example:
# ! /bin/sh
read –p “Enter a password “ pwd_entered
clear
while [ “$pwd_entered “ != “opensesame” ]
do
echo Sorry , try again
read –p “ Enter a password “ pwd_entered
done
echo Correct password entered
until
循環控制語句
until 條件
do
動作
done
until
循環控制語句
until 條件
do
動作
done
注意:
until,當條件爲真時,結束其後的循環。否則,一直循環下去。