linux shell是強大的指令碼程式,文法簡單,下面是一個可執行檔sh指令碼,涵蓋了常用的shell文法和用法,理解它,就等於入門了。
#!/bin/bash
# title :
# date : ?2012/10/18
# author: Made by hfx
echo '[test @test test]# sh [-nvx] scripts
-n :不要執行 scripts ,查詢 scripts 內的文法,若有錯誤則予以列出!
-v :在執行 scripts 之前,先將 scripts 的內容顯示在螢幕上;
-x :將有使用到的 scripts 內容顯示在螢幕上,與 -v 稍微不同 '
echo '逸出字元\後面帶符號'
hello=Hello\ \!\ How\ are\ you\ \?
echo $hello
echo '單引號和雙引號的區別:單引號裡的$只表示$,雙引號裡的$表示變數'
name="V.Bird"
myname1="My name is $name"
myname2='My name is $name'
echo $name
echo $myname1
echo $myname2
echo '顯示聲明變數declare -i'
declare -i a=3
declare -i b=4
#declare -i c=$a * $b
#echo $c
echo '互動'
echo 'cin you name:'
read name
echo 'your name is:'
echo $name
echo 'shell 程式的參數'
echo '$0 表示sh程式後的第0個參數,$1……'
echo '條件陳述式'
echo 'continue please cin:y or Y'
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
echo "script is running..."
else
echo "STOP!"
fi
echo 'case語句'
echo "Press your select one, two, three!"
read choice
case $choice in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}"
#exit 1
esac
echo '迴圈語句for()'
echo '1+……+100'
declare -i s # <==變數宣告
for (( i=1; i<=100; i=i+1 ))
do
s=s+i
done
echo "1+... + 10?0 = $s"
echo 'do...while'
echo "Press Y/y to stop"
until [ "$yn" = "Y" ] || [ "$yn" = "y" ]
do
read yn
done
echo "Stop here"
echo 'test shell end!'