一葉笑天
          雄關(guān)漫道真如鐵, 而今邁步從頭越。 從頭越, 蒼山如海, 殘陽如血。
          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 語法結(jié)構(gòu)

          shbang行

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

          注釋

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

          通配符

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

          輸出顯示

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

          局部變量

          局部變量存在于當(dāng)前shell中。使用set設(shè)置。例如
          set variable_name = value
          set name = "Tom Jones"

          全局變量

          全局變量被稱為環(huán)境變量。例如
          setenv VARIABLE_NAME value
          setenv PRINTER Shakespeare 

          從變量中提取值

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

          讀取用戶輸入

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


          參數(shù)

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

          使用位置參數(shù):

          echo $1 $2 $3

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

          echo $*

          所有的參數(shù)

          使用argv數(shù)組:

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

           

          echo $argv[*]

          所有參數(shù)

          echo $#argv

          參數(shù)號

          數(shù)組

          數(shù)組是一列被空格分隔的字符。使用()把字符都包含進(jìn)去。
          用內(nèi)建的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`"

           

          算術(shù)

          使用@來表示計算結(jié)果的保存變量。例如
          @ 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結(jié)尾. 還可以使用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 結(jié)構(gòu):

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

          循環(huán)

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

          文件測試

          例如:

          –r

          當(dāng)前用戶能讀文件

          –w

          當(dāng)前用戶能寫文件

          –x

          當(dāng)前用戶能執(zhí)行文件

          –e

          文件存在

          –o

          當(dāng)前用戶擁有文件

          –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“表示快速啟動.就是說不要執(zhí)行.cshrc.
          2.注釋行
          3.變量guestfile 被設(shè)置為調(diào)用guests的全路徑
          4.行讀入,如果guests不存在,打印 "guests nonexistent" ,退出腳本。
          5.endif
          6.PLACE 環(huán)境變量
          7.Time局部變量。@是內(nèi)建算術(shù)。
          8.food數(shù)組。
          9.foreach循環(huán)。命令替換`cat $guestfile`.
          10.條件測試
          11.foreach循環(huán)
          12.FINIS是用戶定義的終結(jié)符
          13.shift命令取得下一個person
          14.如果food為空,將會重置。
          15.循環(huán)結(jié)束標(biāo)志
          posted on 2008-06-19 13:52 一葉笑天 閱讀(420) 評論(0)  編輯  收藏 所屬分類: Shell技術(shù)
          主站蜘蛛池模板: 绍兴县| 绍兴市| 茂名市| 探索| 永丰县| 公安县| 和田县| 曲靖市| 香河县| 锡林浩特市| 古交市| 伊春市| 曲周县| 万载县| 辉南县| 沈阳市| 榆中县| 金川县| 尤溪县| 阿克| 射洪县| 枣庄市| 新昌县| 潼关县| 衡东县| 阳原县| 桂阳县| 吉木乃县| 澄城县| 呼和浩特市| 吴桥县| 抚州市| 甘孜县| 黔东| 海盐县| 米脂县| 伊宁市| 额敏县| 麟游县| 彭州市| 林周县|