<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
     

    Korn Shell

    Korn和Bash shells 非常相似.

    Korn語法和結構:

    Shbang

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

    注釋

    行注釋用#符號.例如:

    # This program will test some files

    通配符

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

    echo "How are you?"

    輸出顯示

    輸出屏幕echo和print,例如:

    echo "Who are you?"

    print "How are you?"

    局部變量

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

    variable_name=value

    typeset variable_name=value

    name="John Doe"

    x=5

    全局變量

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

    export VARIABLE_NAME =value

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

    從變量中提取值

    使用$.例如:

    echo $variable_name

    echo $name

    echo $PATH

    讀取用戶輸入

    使用read。例如:

    read name?"What is your name?"

    The prompt is in quotes. After it is displayed, the read command waits for user input

    print -n "What is your name?"

    read name

    read name1 name2 ...

    參數

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

    At the command line:

    $ scriptname arg1 arg2 arg3 ...

    In a script:

    echo $1 $2 $

    位置參數, $1 分配為 arg1, $2 is 分配為arg2, ...

    echo $*

    所有位置參數

    echo $#

    位置參數號

    數組

    Bourne shell 利用位置參數創建字符列表.除位置參數外,Korn shell也支持數組語法,起始位置為0. Korn shell數組用set –A命令創建.例如

    set apples pears peaches

    位置參數

    print $1 $2 $3

    $1 is apples, $2 is pears, $3 is peaches

    set -A array_name word1 word2 word3 ...

    set -A fruit apples pears plums

    Array

    print ${fruit[0]}

    Prints apple

    ${fruit[1]} = oranges

    Assign a new value

    算術

    Korn shell 支持整數算術.typeset i命令會聲明一個整數類型變量. Integer算術能夠在變量上完成。否則,(( )) 語法 (let command)用于算術操作。例如:

    typeset -i variable_name

    聲明integer

    typeset -i num

    num=5+4

    num is declared as an integer

    print $num

    Prints 9

    (( n=5 + 5 ))

    The let command

    print $n

    Prints 10

    命令替換

    像C/TC shells 和Bourne shell,Korn shell提供一種新的語法,將命名放在()中,前面加$.例如:

    variable_name=`command`

    variable_name=$( command )

    echo $variable_name

    echo "Today is `date`"

    echo "Today is $(date)"

    操作符

    Korn shell使用內建的test命令操作符,類似于C 語言操作符.例如:

    相等性:

    比較性:

    =

    string, equal to

    greater than

    !=

    string, not equal to

    >=

    greater than, equal to

    ==

    number, equal to

    less than

    !=

    number, not equal to

    <=

    less than, equal to

    邏輯性:

    &&

    and

    ||

    Or

    !

    Not

    條件語句

    if 語句條件放在()。then關鍵字位于()后. If用fi結束. [[ ]] 用于模式匹配. [ ]用于兼容Bourne shell. Case命令是另外一種if/else.例如:

    The if construct is:

    if command

    then

       block of statements

    fi

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

    if [[ string expression ]]

    then

       block of statements

    fi

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

    if (( numeric expression ))

    then

       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

             ;;

    esac

    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 [[ string expression ]]

    then

       block of statements

    elif [[ string expression ]]

    then

       block of statements

    elif [[ string expression ]]

    then

       block of statements

    else

       block of statements

    fi

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

    if (( numeric expression ))

    then

       block of statements

    elif (( numeric expression ))

    then

       block of statements

    elif (( numeric expression ))

    then

       block of statements

    else

       block of statements

    fi

    循環

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

    while循環 跟隨do。

    until循環。

    for循環。

    select loop is used to provide a prompt (PS3 variable) and a menu of numbered items from which the user inputs a selection The input will be stored in the special built-in REPLY variable. The select loop is normally used with the case command.

    循環控制命令,例如:

    while command

    do

       block of statements

    done

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

    while [[ string expression ]]

    do

       block of statements

    done

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

    while (( numeric expression ))

    do

        block of statements

    done

    until command

    do

    block of statements

    done

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

    until [[ string expression ]]

    do

       block of statements

    done

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

    until (( numeric expression ))

    do

       block of statements

    done

    for variable in word_list

    do

       block of statements

    done

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

    for name in Tom Dick Harry

    do

       print "Hi $name"

    done

    select variable in word_list

    do

       block of statements

    done    

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

    PS3="Select an item from the menu"

    for item in blue red green

       echo $item

    done

    Shows menu:

    1. blue
    2. red
    3. green

    文件測試

    Korn shell使用test command來評估表達式,例如:

    -d

    File is a directory

    -a

    File exists and is not a directory

    –r

    Current user can read the file

    –s

    File is of nonzero size

    –w

    Current user can write to the file

    –x

    Current user can execute the file

    Example 2.5.

       #!/bin/sh

    1   if [ –a 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

    函數

    函數容許定義一段shell,而且給這段代碼給一個名字.有兩種格式:一種來自于Bourne shell,另一種來自于Korn shell.例如:

    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

    }

    Korn Shell腳本:

    例子

    1   #!/bin/ksh

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

    3   guestfile=~/shell/guests

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

        then

            print "${guestfile##*/} non–existent"

            exit 1

        fi

    5   export PLACE="Sarotini's"

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

    7   set -A foods cheese crackers shrimp drinks "hot dogs" sandwiches

    8   typeset -i n=0

    9   for person in $(< $guestfile)

        do

    10      if [[ $person = root ]]

            then

                  continue

            else

                  # Start of here document

    11           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 anythin else you would like to eat? Let

                 me know if you can make it.

                       Hope to see you soon.

                                Your pal,

                                ellie@`hostname`

                 FINIS

    12           n=n+1

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

                 then

    14             set -A foods cheese crackers shrimp drinks "hot dogs"

                   sandwiches

                 fi

              fi

    15 done

        print "Bye..."

    解釋:

    1. 讓Kernal知道在運行Korn shell script.
    2. 注釋

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

    1. 行讀入
    2. 環境變量.
    3. the hour of the day指定給變量Time.
    4. 數組foods賦值,使用 set –A 命令.項開始索引0.
    5. typeset –i 命令創建integer值.
    6. For循環.
    7. 條件測試.
    8. The mail message is sent. The message body is contained in a here document.
    9. 變量n增加1.
    10. 如果數組中的元素號等于變量值,則到達了數據末端.
    11. 結束循環.
    posted on 2008-06-30 10:25 一葉笑天 閱讀(418) 評論(0)  編輯  收藏 所屬分類: Shell技術
    主站蜘蛛池模板: 最近高清国语中文在线观看免费| 日韩在线一区二区三区免费视频| 毛片网站免费在线观看| 久久亚洲精品成人| 一区二区三区视频免费观看| 狼友av永久网站免费观看| 免费人成再在线观看网站| 亚洲AV无码码潮喷在线观看| 一级成人生活片免费看| 久久精品亚洲精品国产色婷| 久久九九AV免费精品| 亚洲日韩精品国产一区二区三区| 久久精品亚洲乱码伦伦中文| 亚洲精品黄色视频在线观看免费资源| 亚洲国产精品久久久久网站| 国产精品深夜福利免费观看| 精品久久久久久亚洲综合网| 免费v片在线观看品善网| 久久国产乱子伦精品免费一 | 国产伦精品一区二区三区免费下载| 中文字幕乱码系列免费| 亚洲精品无码mⅴ在线观看| 在线免费观看亚洲| 菠萝菠萝蜜在线免费视频| 精品亚洲成a人片在线观看少妇 | 久久亚洲中文字幕精品一区| 久久国内免费视频| 成人免费区一区二区三区| 美女被暴羞羞免费视频| 亚洲一级毛片在线观| 国内自产少妇自拍区免费| 一级成人a毛片免费播放| 亚洲视频一区在线| 不卡一卡二卡三亚洲| 日本免费人成视频播放| 无人在线直播免费观看| 桃子视频在线观看高清免费视频| 免费国产黄网站在线看| 亚洲日韩亚洲另类激情文学| 亚洲日本在线观看网址| 亚洲成人午夜在线|