简单的 if/else 语句不起作用

0 bash shell-script

我一直试图让这个基本的代码在 shell 脚本中工作很长时间,但无济于事,代码不起作用!

read dec
if [ dec="Y"]||[dec="Y"]||[dec="y" ]; then
let repeat=1;
else
let repeat=0;
fi
done
Run Code Online (Sandbox Code Playgroud)

小智 5

[和]字符的两边都需要空格,因为它们是命令,而不是运算符。

您也不需要let,并且需要将其dec作为变量读取(即$dec)。

您还可以将变量大写仅用于测试,因此您不需要针对大写(两次!)和小写结果进行测试。

你也不需要done最后,因为没有循环。

read dec
if [[ "${dec^}" = "Y" ]]; then
  repeat=1
else
  repeat=0
fi
Run Code Online (Sandbox Code Playgroud)