2015-02-17 62 views
1

带有多种标志处理我使用的,应使用如下只有一个参数给每个标志被调用的脚本:通过getopt的

./testit.sh -n 123 -t tvar -b bvar -s svar my_program.exe flags to my program 

,所有标志都只是标志的脚本,该脚本将启动my_program.exe与标记flags to my program。被用来做,这是getopts简单方法如下:

#!/bin/bash 
# contents of "test_it.sh" 
echo "\$* = "$* 

getopt_out=`getopt t:T:b:B:s:S:n:N $*` 
echo "getopt_out = "$getopt_out 

echo "\$* = "$* 

set -- `getopt t:T:b:B:s:S:n:N $*` 
echo "\$* = "$* 

while [ $1 != -- ]; do 
    shift 
done 
shift 

echo "**********************************************************************************" 
echo "*** The program I want to run is: "$* 
echo "**********************************************************************************" 

其中(正确)的输出是:

********************************************************************************** 
*** The program I want to run is: my_program.exe flags to my program 
********************************************************************************** 

I,然而,需要发送多个数值选项-n如下:

./testit.sh -n 123 456 789 -t tvar -b bvar -s svar my_program.exe flags to my program 

这会产生不正确的输出:

********************************************************************************** 
*** The program I want to run is: 456 789 my_program.exe flags to my program 
********************************************************************************** 

如何从'testit.sh'中删除那些额外的数字?我能够在脚本的开头处理它们(记录它们的值),以便它们不再需要。由于testit.sh脚本依赖于shift,有没有办法让我完全忽略(抛出)数值而不会搞乱命令的流程,这样shift仍然可以使用?

编辑:进一步调查我的原始脚本,我注意到getopts的输出是不同于我最小的例子中发布的。我在回答中更新了最小示例以及建议的解决方法,尽管我会赞赏其他(可能更具规范和/或更正确的)处理方法)。

+1

如果您需要'123 456 789'是一个单一的参数传递给'-n'标志,那么你需要在调用中引用它,并开始使用'“$ @”而不是'$ *'来支持引用字符串的安全使用等。另外,您需要确保您的'getopt(1)'是util- linux版本并且不以兼容模式运行。 – 2015-02-17 21:35:57

+0

谢谢 - 我会考虑这些事情。然而,对于这个问题,我主要关心如何从'testit.sh'内部处理这个问题。那就是 - 假设我不能改变调用'testit.sh'脚本的方式,但只传递给它的标志。 – drjrm3 2015-02-17 21:41:50

+0

您可以手动走过位置参数中留下的内容,并将想要运行的程序之前发生的任何事情移走。尽管getopt的确如此,但我想我只是看到了,支持一种让它不重新排列非选项参数的方法。如果仍然在最后一个标志后面加上'--'(我看不出它可能如何),那么这里可能会用到它。 – 2015-02-17 21:46:08

回答

0

原来的脚本导致了我实际使用的问题POSIXLY_CORRECT=1,它在输出中稍微改变了--的顺序。我已经发布了新的代码(包括POSIXLY_CORRECT=1),与我一起解决方法的人谁是有兴趣:

#!/bin/sh 
echo "\$* = "$* 

POSIXLY_CORRECT=1 
export POSIXLY_CORRECT 

getopt_out=`getopt t:T:b:B:s:S:n:N: $*` 
echo "getopt_out = "$getopt_out 

set -- `getopt t:T:b:B:s:S:n:N: $*` 
echo "\$* = "$* 

double_dash_too_soon=false 
while [ $1 != -- ]; do 
    echo "\$1 = "$1 
    case $1 in 
    -t) 
     # process this flag 
     shift 
     ;; 
    -b) 
     # process this flag 
     shift 
     ;; 
    -s) 
     # process this flag 
     shift 
     ;; 
    -n) 
     This_num=$2 
     echo "This_num = "$This_num 
     shift 
     while [[ $2 != "-"[a-z,A-Z] ]] ; do 
     echo "Dealing with number!" 
     if [[ $1 == "--" ]]; then 
      echo "WARNING: '--' encountered during numeric reads!!" >&2 
      double_dash_too_soon=true 
     fi 
     shift 
     done 
     ;; 
    esac 
    shift 

    if [ "$double_dash_too_soon" == true ]; then 
    double_dash_too_soon=false 
    echo "(1) \$* -> "$* 
    set -- `getopt t:T:b:B:s:S:n:N $*` 
    echo "(2) \$* -> "$* 
    fi 

done 
shift 

echo "**********************************************************************************" 
echo "*** The program I want to run is: "$* 
echo "**********************************************************************************" 
+1

你读过[BashFAQ/035](http://mywiki.wooledge.org/BashFAQ/035)吗? – 2015-02-18 19:07:23