我有一个类似于下面的代码:
while [ $k = 0 ];
do
if [ $Year1 = $Year2 ] && [ $Month1 = $Month2 ]; then
echo "abcd"
else
echo"erty"错误为行突出显示的行>第144行]:[:预期的参数
发布于 2021-09-15 10:59:05
您的变量中至少有一个包含意外数据(例如,为空、包含空格或换行符),从而导致[命令无法接收预期的参数。
总是引用你的变量("$Year1"而不是$Year1),以避免像这样的意外。
发布于 2021-09-15 10:58:29
while [ "$k" = 0 ]; do if [[ $Year1 == $Year2 && $Month1 == $Month2 ]]; then echo "abcd"; else echo "erty"; fi; doneHelpfull可以是:https://www.shellcheck.net
发布于 2021-09-15 10:50:42
您应该使用==而不是=进行比较,如果您的代码用于整数比较,请随时使用-eq。例如,
if [ $FLAG -eq 1 ];then对于字符串,您可以使用=,例如NOTE :按照Athos爵士的指示进行编辑
if [ "$Year1" = "$Year2" ] && [ "$Month1" = "$Month2" ]; then或者,如果要将变量与字符串进行比较,也可以使用==,
if [ "$STA" == "Completed" ];then添加测试脚本以进一步阐明:
-bash-4.2$ cat string.sh
#!/bin/bash -x
str1="Hello There"
str2="Hello There"
if [ "$str1" = "$str2" ];then
echo "Matched"
else
echo "Not Matched"
fi
if [ "$str1" == "Hello There" ];then
echo "Matched"
else
echo "Not Matched"
fi
-bash-4.2$ ./string.sh
+ str1='Hello There'
+ str2='Hello There'
+ '[' 'Hello There' = 'Hello There' ']'
+ echo Matched
Matched
+ '[' 'Hello There' == 'Hello There' ']'
+ echo Matched
Matched
-bash-4.2$ https://stackoverflow.com/questions/69191536
复制相似问题