我尝试将用户输入与字符串进行比较
这是我的代码
Encode="Encode"
Decode="Decode"
printf "Enter name of file: "
read fileName
printf "Encode Or Decode: "
read EncOrDec
if [ "$Encode"=="$EncOrDec" ]; then
printf "Encode Nice\n"
elif [ "$Decode"=="$EncOrDec" ]; then
printf "Decode Nice\n"
else
printf "Nothing\n"
fi
它总是转到 Encode 语句,为什么?
以及如何修复它
在 bash 中,空格很重要。代替:
if [ "$Encode"=="$EncOrDec" ]; then
With:
if [ "$Encode" = "$EncOrDec" ]; then
如果没有空格,bash 只是测试字符串是否"$Encode"=="$EncOrDec"
是否为空。因为它是never空, 的then
子句总是被执行。
另外,作为一个小细节,当使用[...]
, 指某东西的用途=
字符串相等是 POSIX 标准。 Bash 接受==
but ==
不是标准的,并且不能可靠地移植。
这同样适用于elif
线。代替:
elif [ "$Decode"=="$EncOrDec" ]; then
With:
elif [ "$Decode" = "$EncOrDec" ]; then
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)