linux shell 文法詳解

來源:互聯網
上載者:User

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!'

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.