Bash:使用getopts传递参数



我知道这是一个简单的问题,我一直在研究getopts教程和示例,但我被卡住了。如果你检查这个链接,你会发现在@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 adresin" $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:

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

更新2:

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

在这些行中:

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"

在设置$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 adresin" $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 adresin" $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的变量设置就不会传播。

相关内容

  • 没有找到相关文章

最新更新