zoukankan      html  css  js  c++  java
  • linux shell命令行选项与参数用法详解

    linux shell命令行选项与参数用法详解

    在bash中,可以用以下三种方式来处理命令行参数

         -直接处理:使用$1, $2, ..., $n进行解析, 适合小脚本

         -getopts:单个字符选项的情况(如:-n 10 -f file.txt等选项),能处理绝大多数的情况

         -getopt:可以处理单个字符选项,也可以处理长选项long-option(如:--prefix=/home等),较复杂、功能也更强大

    1、直接处理

    使用以下几个变量进行处理:

    $0   #即命令本身,相当于c/c++中的argv[0]
    $1   #第一个参数
    $2, $3, $4 ...   #第2、3、4个参数,依次类推
    $#   #参数的个数,不包括命令本身
    $@   #参数本身的列表,不包括命令本身
    $*   #和$@相同,但"$*""$@"(加引号)并不同,
         #"$*"将所有的参数解释成一个字符串,而"$@"是一个参数数组

    2、getopts

    • -getopts是bash的内部命令
    • getopts有两个参数,第一个参数是一个字符串,包括字符和“:”
    • 每一个字符都是一个有效的选项(option),如果字符后面带有“:”,表示这个选项有自己的argument,argument保存在内置变量OPTARG中
    • $OPTIND总是存储原始$*中下一个要处理的元素位置
    • 对于while getopts ":a:bc" opt,第一个冒号表示忽略错误

    例如getopts.sh :

    #!/bin/bash
    
    echo original parameters=[$*]
    echo original OPTIND=[$OPTIND]
    while getopts ":a:bc" opt
    do
        case $opt in
            a)
                echo "this is -a option. OPTARG=[$OPTARG] OPTIND=[$OPTIND]"
                ;;
            b)
                echo "this is -b option. OPTARG=[$OPTARG] OPTIND=[$OPTIND]"
                ;;
            c)
                echo "this is -c option. OPTARG=[$OPTARG] OPTIND=[$OPTIND]"
                ;;
            ?)
                echo "there is unrecognized parameter."
                exit 1
                ;;
        esac
    done
    #通过shift $(($OPTIND - 1))的处理,$*中就只保留了除去选项内容的参数,
    #可以在后面的shell程序中进行处理
    shift $(($OPTIND - 1))
    
    echo remaining parameters=[$*]
    echo $1=[$1]
    echo $2=[$2]
    测试结果:
    # ./getopts.sh -a 12 -b -c file1 file2
    original parameters=[-a 12 -b -c file1 file2]
    original OPTIND=[1]
    this is -a option. OPTARG=[12] OPTIND=[3]
    this is -b option. OPTARG=[] OPTIND=[4]
    this is -c option. OPTARG=[] OPTIND=[5]
    remaining parameters=[file1 file2]
    $1=[file1]
    $2=[file2] 

    3、getopt

    说明:

    • getopt是一个外部命令,不是bash内置命令,Linux发行版通常会自带
    • getopt支持短选项和长选项
    • 老版本的getopt问题较多,增强版getopt比较好用,执行命令getopt -T; echo $?,如果输出4,则代表是增强版的
    • 如果短选项带argument且参数可选时,argument必须紧贴选项,如-carg 而不能是-c arg
    • 如果长选项带argument且参数可选时,argument和选项之间用“=”,如--clong=arg而不能是--clong arg

    例如getopt.sh:

    #!/bin/bash
    
    echo original parameters=[$@]
    
    #-o或--options选项后面是可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c,
    #其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的
    #-l或--long选项后面是可接受的长选项,用逗号分开,冒号的意义同短选项。
    #-n选项后接选项解析错误时提示的脚本名字
    ARGS=`getopt -o ab:c:: --long along,blong:,clong:: -n "$0" -- "$@"`
    if [ $? != 0 ]; then
        echo "Terminating..."
        exit 1
    fi
    
    echo ARGS=[$ARGS]
    #将规范化后的命令行参数分配至位置参数($1,$2,...)
    eval set -- "${ARGS}"
    echo formatted parameters=[$@]
    
    while true
    do
        case "$1" in
            -a|--along) 
                echo "Option a";
                shift
                ;;
            -b|--blong)
                echo "Option b, argument $2";
                shift 2
                ;;
            -c|--clong)
                case "$2" in
                    "")
                        echo "Option c, no argument";
                        shift 2  
                        ;;
                    *)
                        echo "Option c, argument $2";
                        shift 2;
                        ;;
                esac
                ;;
            --)
                shift
                break
                ;;
            *)
                echo "Internal error!"
                exit 1
                ;;
        esac
    done
    
    #处理剩余的参数
    echo remaining parameters=[$@]
    echo $1=[$1]
    echo $2=[$2]
    测试结果: 
    #短选项
    # ./getopt.sh -a -b1 -c2 file1 file2
    original parameters=[-a -b1 -c2 file1 file2]
    ARGS=[ -a -b '1' -c '2' -- 'file1' 'file2']
    formatted parameters=[-a -b 1 -c 2 -- file1 file2]
    Option a
    Option b, argument 1
    Option c, argument 2
    remaining parameters=[file1 file2]
    $1=[file1]
    $2=[file2]
    
    #长选项
    ./getopt.sh --along --blong=1 --clong=2 file1 file2
    original parameters=[--along --blong=1 --clong=2 file1 file2]
    ARGS=[ --along --blong '1' --clong '2' -- 'file1' 'file2']
    formatted parameters=[--along --blong 1 --clong 2 -- file1 file2]
    Option a
    Option b, argument 1
    Option c, argument 2
    remaining parameters=[file1 file2]
    $1=[file1]
    $2=[file2]
    
    #长短混合
    # ./getopt.sh -a -b1 --clong=2 file1 file2
    original parameters=[-a -b1 --clong=2 file1 file2]
    ARGS=[ -a -b '1' --clong '2' -- 'file1' 'file2']
    formatted parameters=[-a -b 1 --clong 2 -- file1 file2]
    Option a
    Option b, argument 1
    Option c, argument 2
    remaining parameters=[file1 file2]
    $1=[file1]
    $2=[file2]
     
    对于可选参数出错的情况:
    #短选项和所带argument中间含有空格
    # ./getopt.sh -a -b 1 -c 2 file1 file2
    original parameters=[-a -b 1 -c 2 file1 file2]
    ARGS=[ -a -b '1' -c '' -- '2' 'file1' 'file2']
    formatted parameters=[-a -b 1 -c -- 2 file1 file2]
    Option a
    Option b, argument 1
    Option c, no argument
    remaining parameters=[2 file1 file2]
    $1=[2]
    $2=[file1]
    
    #长选项和所带argument中间含有空格
    # ./getopt.sh --along --blong 1 --clong 2 file1 file2
    original parameters=[--along --blong 1 --clong 2 file1 file2]
    ARGS=[ --along --blong '1' --clong '' -- '2' 'file1' 'file2']
    formatted parameters=[--along --blong 1 --clong -- 2 file1 file2]
    Option a
    Option b, argument 1
    Option c, no argument
    remaining parameters=[2 file1 file2]
    $1=[2]
    $2=[file1]
  • 相关阅读:
    java传值问题.
    方法重载
    java中各种运算符
    java中各种运算符
    java网址
    对象的初始化顺序
    Exploit,shellcode经验技巧杂谈
    关于16位的OS尝试
    window系统下的远程堆栈溢出 《实战篇》
    Exploit,shellcode经验技巧杂谈
  • 原文地址:https://www.cnblogs.com/pugang/p/13067719.html
Copyright © 2011-2022 走看看