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

          2.1 快速瀏覽Shell腳本:
          C shell 和 TC shell 類似于C語言語法
          Bourne shell 是基于Algol語言
          Bash和Korn shells 混合了Bourne和C shells, 但是起源于Bourne shell.
          2.3 C 和TC Shell 語法結構

          shbang行

          "shbang" 行告訴采用哪個shell來解析腳本. 首先是#, !, shell的路徑 例如:#!/bin/csh or #!/bin/tcsh

          注釋

          #符號。例如: # This is a comment

          通配符

           *, ?, and [ ] 用于文件名的擴展.
          ! 是歷史字符
           < , > , >> , <&, and | 用于標準IO的重定向和管道
           為了避免這些符號被shell解析器解釋,shell中必須使用\或者"。例如:
          rm *; ls ??; cat file[1-3]; !!
          echo "How are you?"
          echo Oh boy\!

          輸出顯示

          echo 輸出顯示 echo "Hello to you\!"

          局部變量

          局部變量存在于當前shell中。使用set設置。例如
          set variable_name = value
          set name = "Tom Jones"

          全局變量

          全局變量被稱為環境變量。例如
          setenv VARIABLE_NAME value
          setenv PRINTER Shakespeare 

          從變量中提取值

          為了從變量中提取之,使用$在變量前。例如:
          echo $variable_name
          echo $name
          echo $PRINTER

          讀取用戶輸入

          使用 $< 讀入一行到一個變量中,例如:
          echo "What is your name?"
          set name = $<
                     


          參數

          可以從命令行中傳入參數。兩種方式接受這些參數值:一個是位置參數,另外一個是argv數組。例如:
          % scriptname arg1 arg2 arg3 ...         

          使用位置參數:

          echo $1 $2 $3

          arg1 指定為$1, arg2 to $2, etc.

          echo $*

          所有的參數

          使用argv數組:

          echo $argv[1] $argv[2] $argv[3]

           

          echo $argv[*]

          所有參數

          echo $#argv

          參數號

          數組

          數組是一列被空格分隔的字符。使用()把字符都包含進去。
          用內建的shift命名可以移出左邊的字從list中。
          不同于C, index起始值是1而不是0.
          例如:

          set word_list = ( word1 word2 word3 )

           
          set names = ( Tom Dick Harry Fred )
                      shift names

          從list中刪除Tom

          echo $word_list[1]
                      echo $word_list[2]
                      echo $word_list or $word_list[*]
                      echo $names[1]
                      echo $names[2]
                      echo $names[3]
                      echo $names or echo $names[*]
                      

          顯示第一個
          顯示第二個
          顯示所有

          命令替換

          使用` `例如:

           
          set variable_name=`command` echo $variable_name
           
           
          set now = `date`
                      echo $now
                      echo "Today is `date`"

           

          算術

          使用@來表示計算結果的保存變量。例如
          @ n = 5 + 5
          echo $n

          操作符

          相等性:

          ==

          !=

          Relational:

          >

          greater than

          >=

          greater than or equal to

          <

          less than

          <=

          less than or equal to

          邏輯性:

          &&

          and

          ||

          or

          !

          nSot

          條件語句

          if  then. if 必須用endif結尾. 還可以使用if/else。例如:

          The if construct is:

          if (  expression  ) then
                      block of statements
                      endif
                      

          The if/else construct is:

          if ( expression ) then
                      block of statements
                      else
                      block of statements
                      endif
                      

          The if/else/else if construct is:

          if ( expression ) then
                      block of statements
                      else if ( expression ) then
                      block of statements
                      else if ( expression ) then
                      block of statements
                      else
                      block of statements
                      endif
                      switch ( "$color" )
                      case blue:
                      echo $color is blue
                      breaksw
                      case green:
                      echo $color is green
                      breaksw
                      case red:
                      case orange:
                      echo $color is red or orange
                      breaksw
                      default:
                      echo "Not a valid color"
                      endsw
                      

          switch 結構:

          switch variable_name
                      case constant1:
                      statements
                      case constant2:
                      statements
                      case constant3:
                      statements
                      default:
                      statements
                      endsw

          循環

          兩種類型循環語句:whileforeach
          循環控制中可以使用breakcontinue.例如:
          while ( expression )
                  block of statements
          end
          foreach variable ( word list )
               block of statements
          end
           ------------------------------
           foreach color (red green blue)
               echo $color
            end

          文件測試

          例如:

          –r

          當前用戶能讀文件

          –w

          當前用戶能寫文件

          –x

          當前用戶能執行文件

          –e

          文件存在

          –o

          當前用戶擁有文件

          –z

          文件長度為0

          –d

          文件是目錄

          –f

          文件是普通文件

          2.3.1 C/TC Shell 腳本

          Example 2.2.
          1   #!/bin/csh –f
          2   # The Party Program––Invitations to friends from the "guest" file
          3   set guestfile = ~/shell/guests
          4   if ( ! –e "$guestfile" ) then
          echo "$guestfile:t non–existent"
          exit 1
          5   endif
          6   setenv PLACE "Sarotini's"
          7   @ Time = `date +%H` + 1
          8   set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches )
          9   foreach person ( `cat $guestfile` )
          10      if ( $person =~ root ) continue
          11      mail –v –s "Party" $person << FINIS   # Start of here document 
          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 $food[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`       # or `uname -n`
          12       FINIS
          13       shift food
          14       if ( $#food ==  0 ) then
          set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches )
          endif
          15   end
          echo "Bye..."
          解釋:
          1.告訴kernel,這是C shell腳本. ”–f“表示快速啟動.就是說不要執行.cshrc.
          2.注釋行
          3.變量guestfile 被設置為調用guests的全路徑
          4.行讀入,如果guests不存在,打印 "guests nonexistent" ,退出腳本。
          5.endif
          6.PLACE 環境變量
          7.Time局部變量。@是內建算術。
          8.food數組。
          9.foreach循環。命令替換`cat $guestfile`.
          10.條件測試
          11.foreach循環
          12.FINIS是用戶定義的終結符
          13.shift命令取得下一個person
          14.如果food為空,將會重置。
          15.循環結束標志
          posted on 2008-06-19 13:52 一葉笑天 閱讀(420) 評論(0)  編輯  收藏 所屬分類: Shell技術
          主站蜘蛛池模板: 南开区| 丹棱县| 昌黎县| 衡南县| 九台市| 阿克陶县| 梁平县| 二连浩特市| 镇宁| 怀安县| 景德镇市| 栾城县| 闽清县| 海盐县| 武乡县| 河北省| 东城区| 江阴市| 通州区| 澄迈县| 渑池县| 莱州市| 平遥县| 加查县| 天全县| 湖州市| 玛沁县| 临湘市| 南涧| 建瓯市| 措美县| 盐城市| 洛宁县| 海原县| 莆田市| 英德市| 楚雄市| 左权县| 镶黄旗| 鹰潭市| 平和县|