Bash:使用getopts传递参数

时间:2014-01-17 14:30:02

标签: linux bash shell getopts

我知道这是一个简单的问题,我一直在研究getopts教程和示例,但我被困住了。如果你检查this链接,你会看到在@fedorqui的帮助下,我已经设法编写代码以便在文件中获取数组的元素。

但是,我需要的是参数化输入,这样即使我改变输入顺序,脚本也会继续工作。

这是我写的第一个代码:

#!/bin/bash

file=$3 row=$1 col=$2 str1="Hata: " str2=". satır " str3=". sütun yok"

if [ $row -gt 3 ]
then
    echo $str1$row$str2$col$str3
elif [ $col -gt 3 ]
then
    echo $str1$row$str2$col$str3
else
    awk 'NR==row{print $col}' row=$row col=$col $file
fi 

现在,该脚本的工作方式与$ ./tablooku.sh 3 2 tablom.dat类似,输出为:

~/Desktop $ ./tablooku.sh 3 2 tablom.dat 
Kopenhag

我需要做的是翻译此代码,以便在我输入$ ./tablooku.sh -r 3 -c 2 tablom.dat时,我可以使脚本正常工作。

这是我写的代码,它坚持运行:

#!/bin/bash

str1="Hata: "
str2=". satır "
str3=". sütun yok"

while getopts "r:c:f:" opts
do
            case $opts in
            r)     row=$OPTARG echo "-r was triggered, Parameter: $OPTARG";;
            c)     col=$OPTARG echo "-c was triggered, Parameter: $OPTARG";;
            f)     fi=$OPTARG echo "-f was triggered, Parameter: $OPTARG";;
            \?)     printf "Kullanım: %s -r değer -c değer dosya adresi\n" $0
                          exit 2;;
           esac
done

if [ $row -gt 3 ]
then
    echo $str1$row$str2$col$str3
elif [ $col -gt 3 ]
then
    echo $str1$row$str2$col$str3
else
    awk 'NR==r{print $c}' r=$row c=$col $fi
fi
你能告诉我这里有什么问题吗?当我运行命令时,系统输出为:

~/Desktop $ ./go_test.sh -r 3 -c 2 tablom.dat 
-r was triggered, Parameter: 3
-c was triggered, Parameter: 2
./go_test.sh: line 23: [: -gt: unary operator expected
./go_test.sh: line 26: [: -gt: unary operator expected

提示卡住,我必须使用ctrl+z来打破等待。看起来我没有将文件位置分配给fi,但我无法解决问题

注意:请记住-r是行, - c是列,最后一个参数是不使用任何选项代码的文件位置(例如-f)

更新1:

我将]]]括号[[[一起更改了。现在unary operator expected错误消失了。现在,提示等待并且不打印-f was triggered, Parameter:...我仍然使用ctrl+z来打破等待。

更新2:

根据@grebneke和@TimK的建议,我添加了分号并将fi参数更改为fil。这样,我得到了脚本的工作。但是,还有一个小问题。如果我输入$ ./tablooku.sh -r 3 -c 2 -f tablom.dat,代码就有效。但是,我需要它运行而不必编写-f。有可能吗?

2 个答案:

答案 0 :(得分:1)

在这些方面:

        r)     row=$OPTARG echo "-r was triggered, Parameter: $OPTARG";;
        c)     col=$OPTARG echo "-c was triggered, Parameter: $OPTARG";;
        f)     fi=$OPTARG echo "-f was triggered, Parameter: $OPTARG";;

您只为行的其余部分分配row/col/fi。在分配后添加分号,它将保持分配给脚本的其余部分:

        r)     row=$OPTARG; echo "-r was triggered, Parameter: $OPTARG";;
        c)     col=$OPTARG; echo "-c was triggered, Parameter: $OPTARG";;
        f)     fi=$OPTARG; echo "-f was triggered, Parameter: $OPTARG";;

修改 <deleted, I misunderstood the question>

编辑2 要在使用getopts后获取剩余的参数,您必须shift这样:

while getopts "r:c:" opts
do
...
done

shift $(( $OPTIND-1 ))

# Now, $1, $2 etc are your arguments as you are used to:
$ ./tablooku.sh -r 3 -c 2 tablom.dat
# $1 will be "tablom.dat"

答案 1 :(得分:0)

你在设置$ row,$ col和$ fi的地方错过了一个分号。

        case $opts in
        r)     row=$OPTARG echo "-r was triggered, Parameter: $OPTARG";;
        c)     col=$OPTARG echo "-c was triggered, Parameter: $OPTARG";;
        f)     fi=$OPTARG echo "-f was triggered, Parameter: $OPTARG";;
        \?)     printf "Kullanım: %s -r değer -c değer dosya adresi\n" $0
                      exit 2;;
       esac

应该是(echo之前的分号):

        case $opts in
        r)     row=$OPTARG ; echo "-r was triggered, Parameter: $OPTARG";;
        c)     col=$OPTARG ; echo "-c was triggered, Parameter: $OPTARG";;
        f)     fi=$OPTARG ; echo "-f was triggered, Parameter: $OPTARG";;
        \?)     printf "Kullanım: %s -r değer -c değer dosya adresi\n" $0
                      exit 2;;
       esac

如果没有这些分号,你的命令就不会像你期望的那样运行,因为你可以看到bash -x的输出,并用-gt检查这些行:

bash-[576]$ bash -x foo.bash -r 3 -c 3 -f foo
+ str1='Hata: '
+ str2='. satır '
+ str3='. sütun yok'
+ getopts r:c:f: opts
+ case $opts in
+ row=3
+ echo '-r was triggered, Parameter: 3'
-r was triggered, Parameter: 3
+ getopts r:c:f: opts
+ case $opts in
+ col=3
+ echo '-c was triggered, Parameter: 3'
-c was triggered, Parameter: 3
+ getopts r:c:f: opts
+ case $opts in
+ fi=foo
+ echo '-f was triggered, Parameter: foo'
-f was triggered, Parameter: foo
+ getopts r:c:f: opts
+ '[' -gt 3 ']'
foo.bash: line 18: [: -gt: unary operator expected
+ '[' -gt 3 ']'
foo.bash: line 20: [: -gt: unary operator expected
+ awk 'NR==r{print $c}' r= c=

使用分号:

bash-[574]$ bash -x foo.bash -r 3 -c 3 -f foo
+ str1='Hata: '
+ str2='. satır '
+ str3='. sütun yok'
+ getopts r:c:f: opts
+ case $opts in
+ row=3
+ echo '-r was triggered, Parameter: 3'
-r was triggered, Parameter: 3
+ getopts r:c:f: opts
+ case $opts in
+ col=3
+ echo '-c was triggered, Parameter: 3'
-c was triggered, Parameter: 3
+ getopts r:c:f: opts
+ case $opts in
+ fi=foo
+ echo '-f was triggered, Parameter: foo'
-f was triggered, Parameter: foo
+ getopts r:c:f: opts
+ '[' 3 -gt 3 ']'
+ '[' 3 -gt 3 ']'
+ awk 'NR==r{print $c}' r=3 c=3 foo

再次,用-gt查看这些行。因此,$ row,$ col和$ fi的变量设置不会在没有;的情况下传播。