<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    一葉笑天
    雄關漫道真如鐵, 而今邁步從頭越。 從頭越, 蒼山如海, 殘陽如血。
    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 一葉笑天 閱讀(417) 評論(0)  編輯  收藏 所屬分類: Shell技術
    主站蜘蛛池模板: 午夜理伦剧场免费| 一个人免费视频观看在线www| 日本免费人成黄页网观看视频| 亚洲美女aⅴ久久久91| 污污网站18禁在线永久免费观看| 亚洲成AV人片在WWW色猫咪| 玖玖在线免费视频| 亚洲视频在线视频| 114级毛片免费观看| 亚洲国产品综合人成综合网站| 日韩在线播放全免费| 亚洲乱码在线观看| 日本免费一二区在线电影 | 亚洲白嫩在线观看| 18国产精品白浆在线观看免费| 456亚洲人成影院在线观| 最好免费观看韩国+日本| 久久久久久亚洲精品无码| 亚洲AV无码乱码在线观看| 中国国产高清免费av片| 久久久久亚洲AV无码观看| 午夜无遮挡羞羞漫画免费| 一区二区三区视频免费| 亚洲AV无码久久精品成人| 在线观看成人免费视频不卡| 亚洲av无码一区二区三区在线播放 | 亚洲国产另类久久久精品| 毛片免费全部播放无码| 亚洲精品无码专区在线播放| 亚洲美女高清一区二区三区| 免费人成在线观看网站| 亚洲va久久久久| 亚洲无线码在线一区观看| 国内精自视频品线六区免费| 日日狠狠久久偷偷色综合免费| 亚洲成a人片在线观看无码| 欧洲精品成人免费视频在线观看| 看Aⅴ免费毛片手机播放| 亚洲视频在线观看网址| 五月天婷亚洲天综合网精品偷| 少妇人妻偷人精品免费视频|