如果在Bash中循环不起作用

时间:2014-05-19 14:54:58

标签: bash installer

我一直在使用bash中的安装程序:

#!/bin/bash
echo "Are you sure you want to install the program installCMD? [y/n]"
answer="$@"
if [ $answer = "y" ] then
   echo "Installing Program installCMD"
   wget /usr/bin http://www.chronos.site90.net/DEV/installCMD 
else
   echo "Not installing program installCMD."
fi

每次运行它都会读取错误

Are you sure you want to install the program installCMD? [y/n]
installCMDInstaller: line 7: syntax error near unexpected token `else'
installCMDInstaller: line 7: `else'

任何帮助都会被贬低。 提前谢谢。

3 个答案:

答案 0 :(得分:4)

您需要更正第一个条件部分:

echo "Are you sure you want to install the program installCMD? [y/n]"
answer="$@"
if [ "$answer" == "y" ]; then
   echo "Installing Program installCMD"
   wget /usr/bin http://www.chronos.site90.net/DEV/installCMD 
else
   echo "Not installing program installCMD."
fi

注意测试后的额外引号和分号。

答案 1 :(得分:1)

Use More Quotes

if [ "$answer" = "y" ]
then

答案 2 :(得分:1)

我认为应该有一个;在if语句之后:

#!/bin/bash
echo "Are you sure you want to install the program installCMD? [y/n]"
answer="$@"
if [ $answer = "y" ]; then
   echo "Installing Program installCMD"
   wget /usr/bin http://www.chronos.site90.net/DEV/installCMD 
else
   echo "Not installing program installCMD."
fi

我认为你必须使用==而不是=。