<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
     

    Bash Shell結構

    KornBash shells非常相似,但是還是有一些不同之處。Bash的結構如下所示。

    Bash Shell語法結構

    Shbang

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

    注釋

    行注釋用#符號.例如:# This is a comment

    通配符

    例如*, ?, [ ] 用于文件名擴展。<, >, 2>, >>, | 符號用于IO和重定向和管道。為了保證這些符號不被解析,這個字符要被引起來。 例如:

    rm *; ls ??;  cat file[1-3];

    echo "How are you?"

    輸出顯示

    使用echo命令。使用`或者一對“”通配符。例如:

    echo "How are you?"

    局部變量

    局部變量作用于當前shellshell結束時局部變量失效.例如

    variable_name=value

    declare variable_name=value

    name="John Doe"

    x=5

    全局變量

    全局變量也稱為環(huán)境變量. 例如:內(nèi)建的帶-x選項的聲明函數(shù)也可以設置為環(huán)境變量。可以用export使用。例如:

    export VARIABLE_NAME=value

    declare -x VARIABLE_NAME=value

    export PATH=/bin:/usr/bin:.

    從變量中提取值

    使用$.例如:

    echo $variable_name

    echo $name

    echo $PATH

    讀取用戶輸入

    使用read讀入一行。例如:

    EXAMPLE

    echo "What is your name?"

    read name

    read name1 name2 ...

    參數(shù)

    可以從命令行傳入?yún)?shù)。位置參數(shù)用于從腳本中接收值。例如:

    At the command line:

    $ scriptname arg1 arg2 arg3 ...

    在腳本中:

    echo $1 $2 $3

    位置參數(shù)

    echo $*

    所有位置參數(shù)

    echo $#

    位置參數(shù)號

    數(shù)組

    Bourne shell使用位置參數(shù)創(chuàng)建單詞列表。除了位置參數(shù)外, Bash shell支持數(shù)組語法,起始索引是0 Bash shell數(shù)組使用declare -a 命令創(chuàng)建。例如:

    set apples pears peaches  (positional parameters)

    echo $1 $2 $3

     

    declare -a array_name=(word1 word2 word3 ...)

    declare -a fruit=( apples pears plums )

    echo ${fruit[0]}

    算術

    C/TC shellsBourne shell, UNIX/Linux 命令的輸出可以指定到一個變量。Bash shell提供新的語法. 使用前端加$,例如:

    variable_name=`command`

    variable_name=$( command )

    echo $variable_name

     

    echo "Today is `date`"

    echo "Today is $(date)"

    算術

    Bash shells支持整數(shù)算術。declare -i 命名用于聲明一個整型變量。Korn shelltypeset命令也可以用于向后兼容。

    例如

    declare -i variable_name

    used for bash

    typeset -i variable_name

    can be used to be compatible with ksh

     

    (( n=5 + 5 ))

     

    echo $n

     

     

    操作符

    Bash shell 使用內(nèi)建命令,類似于C語言。

    例如

    相等性:

    邏輯性:

    ==

    equal to

    &&

    and

    !=

    not equal to

    ||

    or

     

     

    !

    not

    關系型:

    > 

    greater than

    >=

    greater than, equal to

    < 

    less than

    <=

    less than, equal to

    條件語句

    If類似于C語言。if endif結束。 [[ ]] 用于模式匹配條件表達式。 [ ] 用于向后兼容Bourne shell例如:

    The if construct is:

    if  command

    then

       block of statements

    fi

     

    if  [[ expression  ]]

    then

       block of statements

    fi

     

    if  (( numeric expression  ))

    then

       block of statements

    else

       block of statements

     fi

     

     

    The if/else construct is:

    if  command

    then

       block of statements

    else

       block of statements

    fi

     

    if  [[ expression ]]

    then

       block of statements

    else

       block of statements

    fi

     

    if  ((  numeric expression ))

    then

       block of statements

    else

       block of statements

    fi

     

    The case construct is:

    case variable_name in

       pattern1)

          statements

             ;;

       pattern2)

          statements

             ;;

       pattern3)

             ;;

    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 matach"

             ;;

    esac

    The if/else/else if construct is:

    if  command

    then

       block of statements

    elif  command

    then

       block of statements

    else if  command

    then

       block of statements

    else

       block of statements

    fi

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

     

    if  [[ expression ]]

    then

       block of statements

    elif  [[  expression ]]

    then

       block of statements

    else if  [[  expression ]]

    then

       block of statements

    else

       block of statements

    fi

     

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

     

    if  ((  numeric expression ))

    then

       block of statements

    elif  ((  numeric expression ))

    then

       block of statements

    else if  ((numeric expression))

    then

       block of statements

    else

       block of statements

    fi

    循環(huán)

    四種循環(huán)while, until, for, select.

    while循環(huán)后跟隨[],do關鍵字,代碼段,結束于done關鍵字。[[ ]]是新的測試操作符,老的[ ]仍舊向后兼容Bourne shell.

    until循環(huán)類似于while循環(huán)。

    for循環(huán)用于遍歷一個字列表。For循環(huán)后跟隨變量名,in關鍵字,字列表,代碼塊,結束于done關鍵字。

    select循環(huán)用于提示菜單選擇。

    循環(huán)控制命令是breakcontinue

    例如

    while command                                until

     command

    do                                           do

       block of statements                     

     block of statements

    done                                         done

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

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

    while [[ string expression ]]                until

     [[ string expression ]]

    do                                           do

       block of statements                       block

     of statements

    done                                         done

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

    while (( numeric expression ))               until

     (( numeric expression ))

    do                                           do

       block of statements                        

     block of statements

    done                                         done

     

    for variable in word_list                  

     select variable in word_list

    do                                           do

       block of statements                       block

     of statements

    done                                         done

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

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

    for color in red green b                   

     PS3="Select an item from the menu"

    do                                           do

     item in blue red green

       echo $color                               echo

     $item

    done                                         done

     

    Shows menu:

    1. blue
    2. red
    3. green

    函數(shù)

    函數(shù)有兩種格式.一種格式來自于Bourne shell, Bash版本使用function關鍵字:例如

    function_name() {

       block of code

    }

    function  function_name {

       block of code

    }

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

    function  lister {

       echo Your present working directory is `pwd`

       echo Your files are:

       ls

    }

     

    Bash Shell腳本例子:

    1   #!/bin/bash

        # GNU bash versions 2.x

    2   # The Party Program––Invitations to friends from the "guest" file

    3   guestfile=~/shell/guests

    4   if [[ ! –e "$guestfile" ]]

        then

    5       printf "${guestfile##*/} non–existent"

            exit 1

        fi

    6   export PLACE="Sarotini's"

    7   (( Time=$(date +%H) + 1 ))

    8   declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)

    9   declare -i  n=0

    10  for person in $(cat $guestfile)

        do

    11      if  [[ $person == root ]]

            then

                  continue

            else

                  # Start of here document

    12            mail –v –s "Party" $person <<- 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

                  ${foods[$n] 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

    13            n=n+1

    14            if (( ${#foods[*]} ==  $n ))

                  then

    15               declare -a foods=(cheese crackers shrimp drinks `"hot dogs"`

                                       sandwiches)

    16            n=0

                  fi

            fi

    17  done

        printf "Bye..."

    解釋

    1. kernel知道在運行Bash shell腳本.
    2. 注釋行
    3. 變量guestfile被設置為文件的全路徑名,叫做guests.
    4. 行讀入
    5. 內(nèi)建函數(shù)printf顯示文件名
    6. 全局環(huán)境變量
    7. 數(shù)字表達式
    8. Bash數(shù)組foods定義
    9. 整數(shù)n定于初始值為0
    10. For循環(huán)
    11. If語句
    12. 發(fā)送mail消息
    13. 整數(shù)n1
    14. If語句
    15. 數(shù)組foods重新分配值
    16. 變量n重新設置回0
    17. 循環(huán)結束
    posted on 2008-07-08 09:38 一葉笑天 閱讀(350) 評論(0)  編輯  收藏 所屬分類: Shell技術
    主站蜘蛛池模板: 成人看的午夜免费毛片| 亚洲国产精品无码第一区二区三区 | 久久亚洲中文字幕无码| 亚洲免费电影网站| 国产亚洲精品资源在线26u| 四只虎免费永久观看| 人禽杂交18禁网站免费| 永久看日本大片免费35分钟| 国产精品免费大片一区二区| 日韩一级片免费观看| 国产尤物在线视精品在亚洲| 亚洲s码欧洲m码吹潮| 亚洲第一视频在线观看免费| 99久久精品免费精品国产| 性xxxxx大片免费视频| 在线看片免费人成视频播| GOGOGO免费观看国语| www成人免费观看网站| 亚洲视频在线免费| 久久综合给合久久国产免费| 大陆一级毛片免费视频观看 | 四虎影视精品永久免费| 亚洲无码在线播放| 亚洲午夜福利精品无码| 亚洲国产精品综合久久一线| 四虎在线免费播放| 久久精品夜色噜噜亚洲A∨| 狠狠综合久久综合88亚洲| 国产日产亚洲系列最新| 亚洲系列国产精品制服丝袜第| 久久亚洲AV无码精品色午夜麻豆| 亚洲美免无码中文字幕在线| 亚洲AV无码一区二区三区久久精品| 中文字幕免费视频精品一| 国产成人无码区免费内射一片色欲 | 亚洲av无码不卡私人影院| 久久精品国产亚洲av水果派 | 亚洲高清中文字幕免费| 午夜在线a亚洲v天堂网2019| 在线观看免费视频一区| 亚洲免费在线观看视频|