中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. 新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > linux shell 腳本入門

        linux shell 腳本入門

        作者: 時間:2007-04-24 來源:網(wǎng)絡 收藏

        本文引用地址:http://www.antipu.com.cn/article/258223.htm

        6. Here documents

        當要將幾行文字傳遞給一個命令時,here documents(譯者注:目前還沒有見到過對該詞適合的翻譯)
        一種不錯的方法。對每個腳本寫一段幫助性的文字是很有用的,此時如果我們四有那個 here documents
        就不必用echo函數(shù)一行行輸出。 一個 Here document 以 開頭,后面接上一個字符串,這個字符串
        還必須出現(xiàn)在here document的末尾。下面是一個例子,在該例子中,我們對多個文件進行重命名,并且
        使用here documents打印幫助:
        #!/bin/sh
        # we have less than 3 arguments. Print the help text:
        if [ $# -lt 3 ] ; then
        cat
        ren -- renames a number of files using sed regular expressions
        USAGE: ren 'regexp' 'replacement' files...
        EXAMPLE: rename all *.HTM files in *.html:
        ren 'HTM$' 'html' *.HTM
        HELP
        exit 0
        fi
        OLD=$1
        NEW=$2
        # The shift command removes one argument from the list of
        # command line arguments.
        shift
        shift
        # $* contains now all the files:
        for file in $*; do
        if [ -f $file ] ; then
        newfile=`echo $file | sed s/${OLD}/${NEW}/g`
        if [ -f $newfile ]; then
        echo ERROR: $newfile exists already
        else
        echo renaming $file to $newfile ...
        mv $file $newfile
        fi
        fi
        done
        這是一個復雜一些的例子。讓我們詳細討論一下。第一個if表達式判斷輸入命令行參數(shù)是
        否小于3個 (特殊變量$# 表示包含參數(shù)的個數(shù)) 。如果輸入?yún)?shù)小于3個,則將幫助文字傳遞
        給cat命令,然后由cat命令將其打印在屏幕上。打印幫助文字后程序退出。 如果輸入?yún)?shù)等
        于或大于3個,我們就將第一個參數(shù)賦值給變量OLD,第二個參數(shù)賦值給變量NEW。下一步,我
        們使用shift命令將第一個和第二個參數(shù)從 參數(shù)列表中刪除,這樣原來的第三個參數(shù)就成為參
        數(shù)列表$*的第一個參數(shù)。然后我們開始循環(huán),命令行參數(shù)列表被一個接一個地被賦值給變量$file。
        接著我 們判斷該文件是否存在,如果存在則通過sed命令搜索和替換來產(chǎn)生新的文件名。然后
        將反短斜線內(nèi)命令結果賦值給newfile。這樣我們就達到了我們的目 的:得到了舊文件名和新
        文件名。然后使用mv命令進行重命名。


        4)函數(shù)
        如果您寫了一些稍微復雜一些的程序,您就會發(fā)現(xiàn)在程序中可能在幾個地方使用了相同的代碼,
        并且您也會發(fā)現(xiàn),如果我們使用了函數(shù),會方便很多。一個函數(shù)是這個樣子的:
        functionname()
        {
        # inside the body $1 is the first argument given to the function
        # $2 the second ...
        body
        }
        您需要在每個程序的開始對函數(shù)進行聲明。

        下面是一個叫做xtitlebar的腳本,使用這個腳本您可以改變終端窗口的名稱。
        這里使用了一個叫做help的函數(shù)。正如您可以看到的那樣,這個定義的函數(shù)被使用了兩次。
        #!/bin/sh
        # vim: set sw=4 ts=4 et:
        help()
        {
        cat
        xtitlebar -- change the name of an xterm, gnome-terminal or kde konsole
        USAGE: xtitlebar [-h] string_for_titelbar
        OPTIONS: -h help text
        EXAMPLE: xtitlebar cvs
        HELP
        exit 0
        }
        # in case of error or if -h is given we call the function help:
        [ -z $1 ] help
        [ $1 = -h ] help
        # send the escape sequence to change the xterm titelbar:
        echo -e 33]0;$107
        #

        在腳本中提供幫助是一種很好的編程習慣,這樣方便其他用戶(和您)使用和理解腳本。
        命令行參數(shù)
        我們已經(jīng)見過$* 和 $1, $2 ... $9 等特殊變量,這些特殊變量包含了用戶從命令
        行輸入的參數(shù)。迄今為止,我們僅僅了解了一些簡單的命令行語法(比如一些強制性的
        參數(shù)和查看幫助的-h選項)。 但是在編寫更復雜的程序時,您可能會發(fā)現(xiàn)您需要更多的
        自定義的選項。通常的慣例是在所有可選的參數(shù)之前加一個減號,后面再加上參數(shù)值 (
        比如文件名)。
        有好多方法可以實現(xiàn)對輸入?yún)?shù)的分析,但是下面的使用case表達式的例子無遺是一個不錯的方法。
        #!/bin/sh
        help()
        {
        cat
        This is a generic command line parser demo.
        USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2
        HELP
        exit 0
        }
        while [ -n $1 ]; do
        case $1 in
        -h) help;shift 1;; # function help is called
        -f) opt_f=1;shift 1;; # variable opt_f is set
        -l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2
        --) shift;break;; # end of options
        -*) echo error: no such option $1. -h for help;exit 1;;
        *) break;;
        esac
        done

        echo opt_f is $opt_f
        echo opt_l is $opt_l
        echo first arg is $1
        echo 2nd arg is $2
        您可以這樣運行該腳本:
        cmdparser -l hello -f -- -somefile1 somefile2
        返回的結果是:
        opt_f is 1
        opt_l is hello
        first arg is -somefile1
        2nd arg is somefile2


        這個腳本是如何工作的呢?腳本首先在所有輸入命令行參數(shù)中進行循環(huán),將輸入?yún)?shù)
        與case表達式進行比較,如果匹配則設置一個變量并且移除該參數(shù)。根據(jù)unix系統(tǒng)的慣例,
        首先輸入的應該是包含減號的參數(shù).



        評論


        相關推薦

        技術專區(qū)

        關閉