标签: getopts

在bourne shell中使用user-defined-function中的getopts

是否可以将命令行参数从bourne脚本中传递到函数中,以便允许getopts处理它们.

我的其余部分很好地打包到函数中,但它开始看起来我必须将参数处理移动到主逻辑中.

以下是它现在的编写方式,但它不起作用:

processArgs()
{
  while getopts j:f: arg
  do
  echo "${arg} -- ${OPTARG}"
     case "${arg}" in
       j)  if [ -z "${filename}" ]; then
           job_number=$OPTARG
           else
              echo "Filename ${filename} already set."
              echo "Job number ${OPTARG} will be ignored.
           fi;;
       f)  if [ -z "${job_number}" ]; then
              filename=$OPTARG
           else
              echo "Job number ${job_number} already set."
              echo "Filename ${OPTARG} will be ignored."
           fi;;
     esac
  done
}

doStuff1
processArgs
doStuff2

是否有可能以一种可以读取脚本args的方式定义函数?这可以通过其他方式完成吗?我喜欢getopts的功能,但看起来在这种情况下,我将不得不牺牲代码之美来获得它.

shell sh getopts

4
推荐指数
1
解决办法
5760
查看次数

如何阻止getopts将空白或null或其他选项用作选项值

我(大概每个人)都不时遇到这个问题,但我自己找不到任何好的解决方法。当getopts寻找一个参数时,即使它是一个选项,它实际上也会接受下一个参数。这是我为阻止该错误所做的事情(代码段):

#!/bin/bash 

function optsGet()
{
    while getopts ":c:f" opt; do
        case $opt in
            c ) [[ -z "${OPTARG}" || "${OPTARG}" == -* ]] \
                && { echo -e "ERROR: Invalid argument\n"; exit 1; }
                CNAME="${OPTARG}.tEsTsTr"
                ;;
            f ) FORCE=true
                ;;
            \?) echo -e "Invalid option: -$OPTARG\n" >&2;;
            : ) echo -e "Missing argument for -$OPTARG\n" >&2; exit 1;;
            * ) echo -e "Unimplemented option: -$OPTARG\n" >&2; exit 1;;
        esac
    done

    shift $(($OPTIND - 1))
}

optsGet "${@}"
echo …
Run Code Online (Sandbox Code Playgroud)

linux bash shell getopt getopts

4
推荐指数
1
解决办法
3227
查看次数

使用--getopts拾取整个单词标记

可以使用getopts拾取全字标志吗?

如下所示:

while getopts ":abc --word" opt; do
    case ${opt} in
        a) SOMETHING
            ;;
        ...
        --word) echo "Do something else."
            ;;
    esac
done
Run Code Online (Sandbox Code Playgroud)

试图拿起那些双破折号。

unix bash getopts

4
推荐指数
3
解决办法
5486
查看次数

在bash中使用getopts解析args的错误

我试图修改要使用的bd脚本getopts.我是bash脚本的新手

我的剧本是

while getopts ":hvis:d:" opt
do
...
done

...

echo $somedirpath
cd "$somedirpath"    
Run Code Online (Sandbox Code Playgroud)

这样做很好

$ ./bd -v -i -s search
Run Code Online (Sandbox Code Playgroud)

要么

$ ./bd -is search -d dir
Run Code Online (Sandbox Code Playgroud)

但是当像这样运行时

$ . ./bd -s search
Run Code Online (Sandbox Code Playgroud)

getopts根本没有阅读论据.我while根据参数在循环中设置的所有变量都没有设置,因此脚本不再起作用.请帮忙!

bash getopts

4
推荐指数
2
解决办法
1267
查看次数

使用 getopts 运行脚本第一次有效,但第二次运行时无效

这是我的脚本。我根据本教程对其进行了改编,因此它不会是脚本本身的错误。(原始脚本也有同样的问题。)

#!/bin/bash

while getopts "a:" opt; do
  case $opt in
    a)
      echo "-a was triggered, Parameter: $OPTARG"
      ;;
  esac
done
Run Code Online (Sandbox Code Playgroud)

这是我的输出:

bash-3.2$ source getopt.sh
bash-3.2$ source getopt.sh -a /dev/null
-a was triggered, Parameter: /dev/null
bash-3.2$ source getopt.sh -a /dev/null
bash-3.2$ 
Run Code Online (Sandbox Code Playgroud)

我已经梳理了互联网,找不到对这种行为的任何解释。

bash shell getopts command-line-arguments

4
推荐指数
1
解决办法
619
查看次数

Bash getopts命令

我在他们的网站上关注IBM的例子:

(清单#5)http://www.ibm.com/developerworks/library/l-bash-parameters/index.html

#!/bin/bash
echo "OPTIND starts at $OPTIND"
while getopts ":pq:" optname
  do
    case "$optname" in
      "p")
        echo "Option $optname is specified"
        ;;
      "q")
        echo "Option $optname has value $OPTARG"
        ;;
      "?")
        echo "Unknown option $OPTARG"
        ;;
      ":")
        echo "No argument value for option $OPTARG"
        ;;
      *)
      # Should not occur
        echo "Unknown error while processing options"
        ;;
    esac
    echo "OPTIND is now $OPTIND"
  done
Run Code Online (Sandbox Code Playgroud)

我想要的是有一个名字超过1个字母的选项.即-pppp和-qqqq而不是-p和-q.

我写了我的程序并实现-help给了我一个问题......

bash getopts

3
推荐指数
1
解决办法
5421
查看次数

函数中调用的 getopts 未拾取标志

我有从我的调用以下脚本.bash_profile

# Set directories based on current path
__set_dirs() {
    currdir=`pwd`
    if [[ ${currdir} =~ "\/path\/to\/main\/(.*)\/working\/([a-z]+)(/?.*)" ]]
    then
        ws=${BASH_REMATCH[1]}
        subdirs=${BASH_REMATCH[3]}
        stgdir=${ts}/${ws}/STAGING${subdirs}
    else
        echo "${currdir} is not a workspace"
        stgdir=""
    fi
}

# Update local version with staging version
upd() {
    __set_dirs
    if [[ -n ${stgdir} ]]
    then
        __overwrite=0
        while getopts "o" opt
        do
            case $opt in
                o)
                    __overwrite=1
                    ;;
                \?)
                    echo "Invalid option: -$OPTARG" >&2
                    ;;
            esac
        done

        echo "Updating ${currdir} with latest from ${stgdir}..."
        if [ ${__overwrite} …
Run Code Online (Sandbox Code Playgroud)

bash getopts

3
推荐指数
1
解决办法
1002
查看次数

How to allow non-optional arguments to be in front of optional arguments using getopts in Bash?

I know how to handle optional arguments using getopts in Bash

#!/bin/bash

while getopts ":a:p:drh" opt; do
  case "$opt" in
    a) echo $OPTARG;;
  esac
done
Run Code Online (Sandbox Code Playgroud)

But if my script expects ./script.sh arg1 [options], how do I tell getopts to skip arg1?

$ ./script.sh -a 1234
1234
$ ./script.sh arg1 -a 1234
$ ./script.sh arg1
$ ./script.sh -a 1234 arg1
1234
Run Code Online (Sandbox Code Playgroud)

As I see I can handle the argument if the argument is put in the last position. What "regex" …

bash getopts

3
推荐指数
1
解决办法
997
查看次数

如何在 shell 中使用互斥标志并添加可选参数标志(使用 getopts)

我正在使用标准 getopts 逻辑。但我想知道如何才能使我提供的选项相互排斥。例如

shell.sh -a SID 
                              <accepted>
shell.sh -b SID
                              <accepted>
shell.sh -ab SID 
               Message- using ab together is the same as running shell.sh     without   any   options supplying  just SID . Help usage < ya da ya > 
shell.sh 
                Please enter SID at the minimum. Usage < ya da ya >
shell.sh SID
               <accepted>
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用如下所示的方法来开发这个逻辑

while getopts ":a:b:" opt; do
  case $opt in
  a ) SID="$OPTARG";;
      set var=1
  b ) SID="$OPTARG";;
      set var=2

 \?) echo "Invalid option: -"$OPTARG"" >&2
        exit …
Run Code Online (Sandbox Code Playgroud)

shell awk case getopts command-line-arguments

3
推荐指数
1
解决办法
5513
查看次数

如何在 getopts 上使用 ${OPTARG}?

我有以下代码:

while getopts ":p:t:n:" o; do
case "${o}" in
    p)
        p=${OPTARG}
        numep=$p
        mkdir $numep
        ;;
    t)
        t=${OPTARG}
        tip=$t
        if [ $tip == "c" ]; then
            touch $numep/$numep.c
            touch $numep/$numep.h
        elif [ $tip == "c++" ]; then
            touch $numep/$numep.cpp
            touch $numep/$numep.h
        fi
            ;;
        n)
            n=${OPTARG}
            nrFis=$n
            if [ $tip == "c" ]; then
                for i in $(seq $n); do
                    touch $numep/src/$numep$i.c
                    touch $numep/inc/$numep$i.h
                done
            elif [ $tip == "c++" ]; then
                for i in $(seq $n); do
                    touch $numep/src/$numep$i.cpp
                    touch $numep/inc/$numep$i.h …
Run Code Online (Sandbox Code Playgroud)

linux bash getopts

3
推荐指数
1
解决办法
7759
查看次数

标签 统计

getopts ×10

bash ×8

shell ×4

command-line-arguments ×2

linux ×2

awk ×1

case ×1

getopt ×1

sh ×1

unix ×1