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

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

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

    Chan Chen Coding...

    Passing arguments to a shell script

    Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. In addition, any arguments you type after the script name on the shell command line are passed to the script as a series of variables.
    The following parameters are recognized:
    $*
    Returns a single string (``$1, $2 ... $n'') comprising all of the positional parameters separated by the internal field separator character (defined by the IFS environment variable).
    $@
    Returns a sequence of strings (``$1'', ``$2'', ... ``$n'') wherein each positional parameter remains separate from the others.
    $1, $2 ... $n
    Refers to a numbered argument to the script, where n is the position of the argument on the command line. In the Korn shell you can refer directly to arguments where n is greater than 9 using braces. For example, to refer to the 57th positional parameter, use the notation ${57}. In the other shells, to refer to parameters with numbers greater than 9, use the shift command; this shifts the parameter list to the left. $1 is lost, while $2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter becomes $9 and can then be referred to.
    $0
    Refers to the name of the script itself.
    $#
    Refers to the number of arguments specified on a command line.
    For example, create the following shell script called mytest:
       echo There are $# arguments to $0: $*    echo first argument: $1    echo second argument: $2    echo third argument: $3    echo here they are again: $@ 
    When the file is executed, you will see something like the following:
       $ mytest foo bar quux    
    There are 3 arguments to mytest: foo bar quux    
    first argument: foo    
    second argument: bar    
    third argument: quux    

    here they are again: foo bar quux 
    $# is expanded to the number of arguments to the script, while $* and $@ contain the entire argument list. Individual parameters are accessed via $0, which contains the name of the script, and variables $1 to $3 which contain the arguments to the script (from left to right along the command line).
    Although the output from $@ and $* appears to be the same, it may be handled differently, as $@ lists the positional parameters separately rather than concatenating them into a single string. Add the following to the end of mytest:
       function how_many {         
            print "$# arguments were supplied."    
        }    
    how_many "$*"    
    how_many "$@" 
    The following appears when you run mytest:
       $ mytest foo bar quux    
    There are 3 arguments to mytest: foo bar quux    
    first argument: foo    
    second argument: bar  
    third argument: quux    
    here they are again: foo bar quux    1 arguments were supplied.    3 arguments were supplied.


    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-02-29 21:58 Chan Chen 閱讀(195) 評論(0)  編輯  收藏 所屬分類: Linux

    主站蜘蛛池模板: 亚洲国产理论片在线播放| 亚洲福利精品电影在线观看| 亚洲AV综合色区无码一区爱AV| 一级成人a免费视频| 亚洲综合色视频在线观看| 特级毛片爽www免费版| 免费在线观看视频a| 好男人视频在线观看免费看片| 亚洲成人黄色网址| 欧亚精品一区三区免费| 亚洲av无码成人精品区一本二本| 在线观看成人免费| 国产精品亚洲综合网站| 亚洲一级片免费看| 最好免费观看高清在线| 中文字幕亚洲综合久久| 国产乱码免费卡1卡二卡3卡| 亚洲高清有码中文字| 免费一级毛片一级毛片aa| 国产精品免费一区二区三区| 亚洲av无码不卡一区二区三区| 1000部夫妻午夜免费| 亚洲精品永久在线观看| 亚洲国产精品日韩专区AV| 中文无码成人免费视频在线观看| 2022年亚洲午夜一区二区福利| 啦啦啦高清视频在线观看免费| 免费观看又污又黄在线观看| 亚洲国产精品福利片在线观看 | 国产久爱免费精品视频| 亚洲国产成人片在线观看无码| 国产成在线观看免费视频| 美女免费视频一区二区| 亚洲AV午夜成人片| 午夜免费不卡毛片完整版| 国产无限免费观看黄网站| 亚洲成人福利在线观看| 亚洲精品高清在线| 成人免费观看一区二区| 成人免费观看男女羞羞视频| 亚洲明星合成图综合区在线|