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

时间:2015-12-23 20:59:49

标签: bash

我有一个bash脚本,我需要一些参数。用法应该

import inspect

class Foo:

    def __init__(self):
        self._x = 3

    @property
    def x(self):
        curframe = inspect.currentframe()
        calframe = inspect.getouterframes(curframe, 2)      
        print(calframe[1][2])
        return self._x

f = Foo()
def bar():
    print( f.x )    
    # if instead we use the print statement on the next line,
    # we note that there is one more stack frame. In no universe
    # is this particularly impact, but in a closed test case
    # we would be able to determine which one was being used
    #print( getattr(f,'x') )

bar()

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

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

./script.sh --help

我怎么能做到这一点?

4 个答案:

答案 0 :(得分:0)

这只是我的头脑:

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

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

答案 1 :(得分:0)

几乎任何用于从源代码构建软件的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

答案 2 :(得分:0)

对于你的两个例子

./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

答案 3 :(得分:0)

我终于成功了!基本版本是:

#!/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"

无论如何,谢谢你的帮助:)