繁体   English   中英

如何在bash中要求命令行参数?

[英]How to require command line parameters in bash?

我有一个 bash 脚本,我需要在其中设置一些参数。 用法应该在:

./script.sh --scan [scan type] [keyword]

或者

./script.sh --help

在示例中它应该是这样的:

$ ./script.sh 
[Usage]
$ ./script.sh --scan
Specify scan type
$ ./script.sh --help --scan
[Usage]
$ ./script.sh --scan short
Specify keyword to search
$ ./script.sh --scan short keyword
[Starts short scanning for "keyword" - go to function where my script is, blah, blah]
$ ./script.sh keyword --scan short
[As above]
$ ./script.sh keyword
[Usage]

我怎么能做到这一点?

这只是我的头顶:

if [ "$1" = "--scan" ] ; then
    if [ "$#" -ge 2 ] ; then
        _SCANTYPE=$2
        if [ "$#" -ge 3 ] ; then
            _KEYWORD=$3
        else
            echo -n "Specify keyword to search: "
            read _KEYWORD
        fi
    else
        echo -n "Specify scan type: "
        read _SCANTYPE
        echo -n "Specify keyword to search: "
        read _KEYWORD
    fi
    # Actual scan code
else
    # Display usage info
fi

当然,这是非常基本的,但希望它能让你开始。

几乎所有从源代码构建软件的Configure脚本都会有你想要的。 来自 perl:

: option parsing
while test $# -gt 0; do
    case "$1" in
    -d) shift; fastread=yes;;
    -e) shift; alldone=cont;;
    -f)
        shift
        cd ..
        if test -r "$1"; then
            config_sh="$1"
        else
            echo "$me: cannot read config file $1." >&2
            error=true
        fi
        cd UU
        shift;;
    --help|\
    -h) shift; error=true;;
    -r) shift; reuseval=true;;
    -s) shift; silent=true; realsilent=true;;
    -E) shift; alldone=exit;;
    -K) shift; knowitall=true;;
    -O) shift; override=true;;
    -S) shift; silent=true; extractsh=true;;
    -D)
        shift
        case "$1" in
        *=)
            echo "$me: use '-U symbol=', not '-D symbol='." >&2
            echo "$me: ignoring -D $1" >&2
            ;;
        *=*) echo "$1" | \
                sed -e "s/'/'\"'\"'/g" -e "s/=\(.*\)/='\1'/" >> optdef.sh;;
        *) echo "$1='define'" >> optdef.sh;;
        esac
        shift
        ;;
    -U)
        shift
        case "$1" in
        *=) echo "$1" >> optdef.sh;;
        *=*)
            echo "$me: use '-D symbol=val', not '-U symbol=val'." >&2
            echo "$me: ignoring -U $1" >&2
            ;;
        *) echo "$1='undef'" >> optdef.sh;;
        esac
        shift
        ;;
    -A)
        shift
        xxx=''
        yyy="$1"
        zzz=''
        uuu=undef
        case "$yyy" in
            *=*) zzz=`echo "$yyy"|sed 's!=.*!!'`
                 case "$zzz" in
                 *:*) zzz='' ;;
                 *)   xxx=append
                      zzz=" "`echo "$yyy"|sed 's!^[^=]*=!!'`
                      yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
                 esac
                 ;;
            esac
            case "$xxx" in
            '')  case "$yyy" in
                 *:*) xxx=`echo "$yyy"|sed 's!:.*!!'`
                      yyy=`echo "$yyy"|sed 's!^[^:]*:!!'`
                      zzz=`echo "$yyy"|sed 's!^[^=]*=!!'`
                      yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
                 *)   xxx=`echo "$yyy"|sed 's!:.*!!'`
                      yyy=`echo "$yyy"|sed 's!^[^:]*:!!'` ;;
                 esac
                 ;;
            esac
        case "$xxx" in
        append)
        echo "$yyy=\"\${$yyy}$zzz\""    >> posthint.sh ;;
        clear)
        echo "$yyy=''"          >> posthint.sh ;;
        define)
            case "$zzz" in
        '') zzz=define ;;
        esac
        echo "$yyy='$zzz'"      >> posthint.sh ;;
        eval)
        echo "eval \"$yyy=$zzz\""   >> posthint.sh ;;
        prepend)
        echo "$yyy=\"$zzz\${$yyy}\""    >> posthint.sh ;;
        undef)
            case "$zzz" in
        '') zzz="$uuu" ;;
        esac
        echo "$yyy=$zzz"        >> posthint.sh ;;
            *)  echo "$me: unknown -A command '$xxx', ignoring -A $1" >&2 ;;
        esac
        shift
        ;;
    -V) echo "$me generated by metaconfig 3.5 PL0." >&2
        exit 0;;
    --) break;;
    -*) echo "$me: unknown option $1" >&2; shift; error=true;;
    *) break;;
    esac
done

对于你的两个例子

./script.sh --scan [scan type] [keyword]

或者

./script.sh --help

尝试这个

#!/bin/bash
usageMsg="usage:ScriptName --scan [scan type] [keyword] OR --help"

case $# in
  [123] ) : nothing_may_be_OK ;;
  0 ) # no args, display usageMsg
      echo "$usageMsg" >&2 ; exit 1 ;;
esac

case "$1" in
  --[Hh][Ee][Ll][Pp] ) echo "$usageMsg" >&2 ; exit 1 ;;
  --[Ss][Cc][Aa][Nn] )
      shift
      case $# in
        0) # using defaults 
           scanType="PrimaryScan" # change to your default scanType
           keyword="PrimaryKeyWord" # again, change to your default value
        ;;
        1) 
          echo "$usageMsg" >&2
          echo "must provide both [scan type] and [keyword] arguments. Cant continue" >&2 
          exit 1
        ;;
        2)
           scanType="$1"
           keyword="$2"
        ;;
      esac   # case $#
  ;;
esac          # case $1

#restof your code goes here
echo "running scanner with scanType=$scanType and keyWord=$keyword"
echo ". . . . ."
exit 0

我猜你的使用模式意味着--scan或高级扫描(详细信息),即--scan [scan type] [keyword]

如果您真的打算让用户选择性地添加[scan type] AND/OR [keyword]那么您将需要进一步使用上述代码中的技术。

IHTH

我终于做到了! 基本版本是:

#!/bin/bash
function usage
{
        echo "Usage: usage"
        exit
}
while [ $# -gt 0 ]; do
        case $1 in
                -h|--help)
                        usage
                        ;;
                -s|--scan)
                        scantype="$2"
                        shift
                        ;;
                -*)
                        usage
                        ;;
                *)
                        keyword="$1"
                        ;;
        esac
        shift
done
if [ -z "$scantype" ]; then
        echo "no scantype"
        exit
fi
if [[ "$scantype" != "full" && "$scantype" != "long" && "$scantype" != "extended" && "$scantype" != "short" ]]; then
        echo "invalid scantype"
        exit
fi
if [ -z "$keyword" ]; then
        echo "nokeyword"
        exit
fi
echo "scantype: $scantype"
echo "keyword: $keyword"

无论如何感谢您的帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM