从脚本不期望的输出 [英] not expected output from script
问题描述
当我运行该code与参数如 ./ getopts的-a
它打印喜-a
虽然进出料将嗨你好
。任何想法什么问题呢?
#!/斌/庆典而getopts的一个:名称
做
案例$名称
一)aopt = $ OPTARG ;;
*)回声无效ARG;;
ESAC
DONE如果[! -z $ aopt]];然后
回声$ aopt
科幻移$(($ OPTIND - 1))#exit 0
它看起来像被捆绑到你的 -a
选项的唯一事情就是喜
。如果你只需要拉你好
与 -a一起喜
你可以从$ 3命令行拉。
,而getopts的A:名称;做
案例$名称
一)aopt = $ OPTARG ;;
*)回声无效ARG;;
ESAC
DONE
如果[! -z $ aopt]];然后
回声$ aopt $ 3
科幻
移$(($ OPTIND - 1))
请注意,在你身边需要重复一下。我会建议采取一看的http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt关于getopt的一个漂亮的快速阅读。
when i run this code with an argument e.g ./getopts -a
it prints "hi -a"
though the expected out would be "hi hello"
. Any ideas whats going wrong?
#!/bin/bash
while getopts a:name
do
case $name in
a)aopt=$OPTARG;;
*)echo "Invalid arg";;
esac
done
if [[ ! -z $aopt ]]; then
echo $aopt
fi
shift $(($OPTIND - 1))
#exit 0
It looks like the only thing being tied to your -a
option is hi
. If you simply need to pull hello
along with -a hi
you can just pull it from the command line with $3.
while getopts "a:" name; do
case $name in
a)aopt=$OPTARG;;
*)echo "Invalid arg";;
esac
done
if [[ ! -z $aopt ]]; then
echo "$aopt $3"
fi
shift $(($OPTIND - 1))
Please note the "" around what you need to echo. I would suggest taking a look at http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt for a nice quick read on getopt.
这篇关于从脚本不期望的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!