参考自鸟哥的私房菜——
[url]http://linux.vbird.org/linux_basic/0340bashshell-scripts.php[/url]
[b]输入变量并提示[/b]
[b]改变类型[/b]
[quote]# declare [-aixr] variable
選項與參數:
-a :將後面名為 variable 的變數定義成為陣列 (array) 類型
-i :將後面名為 variable 的變數定義成為整數數字 (integer) 類型
-x :用法與 export 一樣,就是將後面的 variable 變成環境變數;
-r :將變數設定成為 readonly 類型,該變數不可被更改內容,也不能 unset
# sum=100+300+50
# echo $sum
100+300+50 <==咦!怎麼沒有幫我計算加總?因為這是文字型態的變數屬性啊!
# declare -i sum=100+300+50[/quote]
[b]if语句[/b]
[b]case语句[/b]
[b]for语句[/b]
[url]http://linux.vbird.org/linux_basic/0340bashshell-scripts.php[/url]
[b]输入变量并提示[/b]
read -p "Please input your first name: " -t 30 firstname # 请在30s内输入名字
[b]改变类型[/b]
[quote]# declare [-aixr] variable
選項與參數:
-a :將後面名為 variable 的變數定義成為陣列 (array) 類型
-i :將後面名為 variable 的變數定義成為整數數字 (integer) 類型
-x :用法與 export 一樣,就是將後面的 variable 變成環境變數;
-r :將變數設定成為 readonly 類型,該變數不可被更改內容,也不能 unset
# sum=100+300+50
# echo $sum
100+300+50 <==咦!怎麼沒有幫我計算加總?因為這是文字型態的變數屬性啊!
# declare -i sum=100+300+50[/quote]
[b]if语句[/b]
# 多個條件判斷 (if ... elif ... elif ... else) 分多種不同情況執行
if [ 條件判斷式一 ]; then
當條件判斷式一成立時,可以進行的指令工作內容;
elif [ 條件判斷式二 ]; then
當條件判斷式二成立時,可以進行的指令工作內容;
else
當條件判斷式一與二均不成立時,可以進行的指令工作內容;
fi
[b]case语句[/b]
case $1 in
"hello")
echo "Hello, how are you ?"
;;
"")
echo "You MUST input parameters, ex> {$0 someword}"
;;
*) # 其實就相當於萬用字元,0~無窮多個任意字元之意!
echo "Usage $0 {hello}"
;;
esac
[b]for语句[/b]
for animal in dog cat elephant
do
echo "There is ${animal}.... "
done
for (( i=1; i<=$nu; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+$nu' is ==> $s"