一葉笑天
          雄關漫道真如鐵, 而今邁步從頭越。 從頭越, 蒼山如海, 殘陽如血。
          posts - 73,comments - 7,trackbacks - 0
          Bourne shell語法和結構:

          Shbang

          "shbang" 是腳本起始行,告訴kernel那個shell解析. #!位于行頭。#!/bin/sh

          注釋

          行注釋用#符號.例如:

          # this text is not

          # interpreted by the shell

          通配符

          *,?, 和 [ ]用于文件名擴展.例如<, >, 2>, >>, 和 | 用于IO和重定向.為了保證這些符號不被解析,這個字符要被引起來。 例如:文件名擴展:rm *; ls ??; cat file[1-3];引用保護:echo "How are you?"

          輸出顯示

          輸出屏幕:echo "What is your name?"

          局部變量

          局部變量作用于當前shell,shell結束時局部變量失效.例如variable_name=value 

          name="John Doe"  

          x=5

          全局變量

          全局變量也稱為環境變量. 例如:

          VARIABLE_NAME=value

          export VARIABLE_NAME

          PATH=/bin:/usr/bin:.

          export PATH

          從變量中提取值

          使用$.例如:

          echo $variable_name

          echo $name

          echo $PATH

          讀取用戶輸入

          使用read。例如:

          echo "What is your name?"

          read name

          read name1 name2 ...

          參數 (位置參數)

          可以從命令行傳入參數。位置參數用于從腳本中接收值。例如:

          $ scriptname arg1 arg2 arg3 ...

          腳本中:

          echo $1 $2 $3

          位置參數

          echo $*

          所有的位置參數

          echo $#

          位置參數號

          數組 (位置參數)

          Bourne shell不支持數組, 但是詞列表可以用位置參數取得. 使用內建set命令來建造列表。用shift移除左邊第一個詞。位置索引從1開始.例如:

          set word1 word2 word3

          echo $1 $2 $3

          顯示 word1, word2, and word3

          set apples peaches plums

          shift

          移走apples

          echo $1

          顯示列表第一個值

          echo $2

          顯示列表第二個值

          echo $*

          顯示所有值

          命名替代

          為了制定命令的輸出或者在字符串中使用命令,使用反引號`.例如:variable_name=`command`

          echo $variable_name

          now=`date`

          echo $now

          echo "Today is `date`"

          算術

          Bourne shell不支持算式.要使用命令來完成計算.例如:

          n=`expr 5 + 5`

          echo $n

          操作符

          Bourne shell使用內建test命令操作符來測試數字和字符串.例如:

          相等:

          =

          字符串

          !=

          字符串

          -eq

          數值

          -ne

          數值

          邏輯:

          -a

          And

          -o

          Or

          !

          Not

          關系的:

          -gt

          大于

          -ge

          大于等于

          -lt

          小于

          -le

          小于等于

          條件語句

          If結構. 也可以包含在[]中. then用在結尾. If必須用fi結束.例如:

          The if construct is:

          The if/else construct is:

          if command

          then

             block of statements

          fi

          if [ expression ]

          then

             block of statements

          fi

          The if/else/else if construct is:

          if command

          then

             block of statements

          elif command

          then

             block of statements

          elif command

          then

             block of statements

          else

             block of statements

          fi

          --------------------------

          if [ expression ]

          then

             block of statements

          elif [ expression ]

          then

             block of statements

          elif [ expression ]

          then

             block of statements

          else

             block of statements

          fi

          if [ expression ]

          then

             block of statements

          else

             block of statements

          fi

          The case command construct is:

          case variable_name in

             pattern1)

                statements

                   ;;

             pattern2)

                statements

                   ;;

             pattern3)

                   ;;

             *) default value

                   ;;

          esac

          case "$color" in

             blue)

                echo $color is blue

                ;;

             green)

                echo $color is green

                ;;

             red|orange)

                echo $color is red or orange

                ;;

             *) echo "Not a color" # default

          Esac

          循環

          3種循環類型: while, until 和 for.

          例如:

          while command

          do

             block of statements

          done

          while [ expression ]

          do

             block of statements

          done

          until command                                for

           variable in word1 word2 word3 ...

          do                                           do

             block of statements                          

           block of statements

          done                                         done

          until [ expression ]

          do

             block of statements

          done

          文件測試

          Bourne shell使用內建的測試命令。例如:

          -d

          文件是目錄

          -f

          文件存在而且不是目錄

          –r

          當前的用戶讀文件

          –s

          文件是非0大小

          –w

          當前用戶可以寫文件

          –x

          當前用戶可以執行文件

          Example 2.3.

              #!/bin/sh

          1   if [ –f file ]

              then

                   echo file exists

              fi

          2   if [ –d file ]

              then

                    echo file is a directory

              fi

          3   if [ -s file ]

              then

                   echo file is not of zero length

              fi

          4   if [ -r file -a -w file ]

              then

                    echo file is readable and writable

              fi

          函數

          用一個給定的名字定義代碼. Bourne shell引入了函數的概念. C 和 TC不容許有函數.例如:

          function_name() {

             block of code

          }

          -----------------------

          lister() {

             echo Your present working directory is `pwd`

             echo Your files are:

             ls

          }

          Bourne shell 例子:
          1   #!/bin/sh
          2   # The Party Program––Invitations to friends from the "guest" file
          3   guestfile=/home/jody/ellie/shell/guests
          4   if [ ! –f "$guestfile" ]
          5   then
          6        echo "`basename $guestfile` non–existent"
          7        exit 1
          8   fi
          9   PLACE="Sarotini's"; export PLACE
          10 Time=`date +%H`
              Time=`expr $Time + 1`
          11 set cheese crackers shrimp drinks "hot dogs" sandwiches
          12 for person in `cat $guestfile`
              do
          13       if [ $person =~ root ]
                   then
          14             continue
          15       else
          16       #     mail –v –s "Party" $person <<- FINIS
          17             cat <<-FINIS
                         Hi ${person}! Please join me at $PLACE for a party!
                         Meet me at $Time o'clock.
                         I'll bring the ice cream. Would you please bring $1 and
                         anything else you would like to eat? Let me know if you
                         can make it. Hope to see you soon.
                             Your pal,
                             ellie@`hostname`
                         FINIS
          18             shift
          19             if [ $# –eq 0 ]
                         then
          20                set cheese crackers shrimp drinks "hot dogs" sandwiches
                         fi
                   fi
          21   done
               echo "Bye..."

          解釋:

          1.    讓kernel知道是在運行Bourne shell。

          2.    注釋。

          3.    變量guestfile被設置為文件的全路徑名,叫做guests.

          4.    行讀入。

          5.    then關鍵字.

          6.    UNIX basename命名刪除所有不再搜索路徑里的文件.

          7.    如果文件不存在,程序退出。

          8.    fi關鍵字表示if結束

          9.    變量指定值place和time. PLACE是環境變量

          10.Time變量值做了命名替換

          11.foods列表指定特定的變量。

          12.for循環。

          13.如果變量person匹配user root,loop控制將會到達for循環的頭,處理下一個person。

          14.continue語句是loop控制從12行開始,而不是16行。

          15.else語句。

          16. 

          17.next語句,使用cat命令。

          18.,循環,移動到下一個人。

          19.$#值是位置參數號.如果號碼為0,則food列表為空。

          20.重置food列表。

          21.done關鍵字表明for循環結束。

          posted on 2008-06-25 18:29 一葉笑天 閱讀(358) 評論(0)  編輯  收藏 所屬分類: Shell技術
          主站蜘蛛池模板: 喀喇沁旗| 东光县| 淮滨县| 含山县| 奉化市| 迁安市| 射洪县| 焦作市| 晋中市| 平和县| 邵阳县| 历史| 安泽县| 萨嘎县| 崇信县| 兴宁市| 新龙县| 万盛区| 酒泉市| 安康市| 通江县| 江陵县| 德安县| 平顺县| 哈密市| 五华县| 乐陵市| 呼和浩特市| 青浦区| 玉龙| 通城县| 夏河县| 龙里县| 鄂托克前旗| 买车| 常熟市| 方城县| 西畴县| 五台县| 瑞安市| 南江县|