<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技術
    主站蜘蛛池模板: 两个人看的www免费| 亚洲精品动漫免费二区| 四虎永久在线观看免费网站网址 | 免费人成再在线观看网站 | 少妇性饥渴无码A区免费 | 国产国产人免费视频成69大陆| 亚洲av中文无码乱人伦在线r▽| 国产亚洲情侣久久精品| 亚洲中文无码永久免费| 男女交性无遮挡免费视频| 亚洲五月激情综合图片区| 成人av片无码免费天天看| 国产免费变态视频网址网站| 亚洲av永久无码一区二区三区 | 国产yw855.c免费视频| 久久精品中文字幕免费| 亚洲中文字幕久久精品无码喷水 | 最近中文字幕mv免费高清视频7 | 蜜芽亚洲av无码一区二区三区| 成年女人永久免费观看片| 亚洲人成网站免费播放| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲宅男永久在线| 亚洲一区二区三区免费在线观看 | 久久亚洲2019中文字幕| 九九热久久免费视频| 亚洲自偷自偷偷色无码中文| 三年片在线观看免费观看高清电影| 亚洲精品久久无码| 亚洲人成依人成综合网| 亚洲国产日韩在线观频| 国产精品免费看久久久香蕉| 亚洲日本乱码在线观看| 国产高清不卡免费视频| 国产精品亚洲专区在线播放| 亚洲人成在线中文字幕| 免费A级毛片无码久久版| a级黄色毛片免费播放视频| 精品国产亚洲第一区二区三区| 亚洲中文字幕久久精品无码APP| 精品久久洲久久久久护士免费|