我正在为bash中的程序编写安装脚本。安装的第一部分检测程序是否已安装。这是通过指定目录中存在的文件来检测的。那部分工作正常。但是,如果旧版本存在,我希望用户能够升级到新版本。我以为我的代码是正确的,但它并不常用。 / var / www / html / gvsms中有一个名为.version的文件,该文件只包含文件中的程序版本。我想将该数字与安装程序的数量进行比较。但是,我不确定如何在if语句中。 If中可以有多个Ifs吗?这就是我到目前为止所做的:
$installerver=1.1
$installedver=${cat /var/www/html/gvsms/.version}
echo "Installation beginning...."
echo " "
echo " "
echo -n "Checking to see if GVoice SMS Notification System was previously installed..."
if [ -a /var/www/html/gvsms/index.php ]; then
echo "Yes."
echo ""
echo "I have detected a previous installation of GVoice SMS Notification System."
if [ $installerver == $installedver ]; then
echo "The GVoice SMS Notification System is currently installed and is the same version as this installer. If you were trying to upgrade, please check the installer you downloaded and try again."
exit
fi
elif [ $installedver == "0.5" || $installedver == "1.0"]; then
while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do
echo "Would you like to upgrade? Yes or no."
read -r upgrade
echo "$upgrade upgrade?"
echo "Is this correct? (Yes or No)"
read yn
done
echo "Upgrade proceeding..."
fi
echo "No."
echo "I have not detected a previous installation of GVoice SMS Notification System."
read -p "To proceed with installation at your own risk, press Enter. Otherwise Ctrl-C."
现在程序出错了:
./test.x: line 1: =1.1: command not found
./test.x: line 2: ${cat /var/www/html/gvsms/.version}: bad substitution
程序应该运行的方式是,如果检测到文件,安装的版本不是(或小于)与安装程序相同,并且用户说是升级,则应运行升级命令。如果用户拒绝升级,则退出脚本。
如果程序已安装且与安装程序版本相同,则显示错误消息并退出。
如果尚未安装该程序,请跳过再次确认安装并继续正常安装。
这可能吗? 谢谢!
答案 0 :(得分:3)
将shebang行添加到脚本的顶部:
#!/bin/bash
变量分配没有美元符号:
installerver=1.1
子shell命令的变量赋值使用括号而不是大括号:
installedver=$(cat /var/www/html/gvsms/.version)
if block
fi
else
阻止[ … ]
和[[ … ]]
这将使您的代码运行:
#!/bin/bash
installerver="1.1"
echo "Installation beginning...."
echo " "
echo " "
echo -n "Checking to see if GVoice SMS Notification System was previously installed..."
if [ -e "/var/www/html/gvsms/index.php" -a -r "/var/www/html/gvsms/.version" ]; then
echo "Yes."
echo ""
echo "I have detected a previous installation of GVoice SMS Notification System."
if [ "$(cat /var/www/html/gvsms/.version)" == "$installedver" ]; then
echo "The GVoice SMS Notification System is currently installed and is the same version as this installer. If you were trying to upgrade, please check the installer you downloaded and try again."
exit
elif [ "$installedver" == "0.5" || "$installedver" == "1.0" ]; then
while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do
echo "Would you like to upgrade? Yes or no."
read -r upgrade
echo "$upgrade upgrade?"
echo "Is this correct? (Yes or No)"
read yn
done
echo "Upgrade proceeding..."
fi
else
echo "No."
echo "I have not detected a previous installation of GVoice SMS Notification System."
read -p "To proceed with installation at your own risk, press Enter. Otherwise Ctrl-C."
fi
答案 1 :(得分:0)
您的作业有语法错误。分配给变量时,不要设置美元符号$
。对于命令替换,请使用括号$(...)
。
installerver=1.1
installedver=$(cat /var/www/html/gvsms/.version)
大括号用于简单的变量替换,如echo "You bought $qty ${item}s."
。