我正在为 bash 中的程序编写安装脚本。安装的第一部分检测程序是否已安装。这是通过指定目录中是否存在文件来检测的。这部分工作正常。但是,如果旧版本存在,我希望用户能够升级到新版本。我以为我的代码是正确的,但它并不完全有效。在/var/www/html/gvsms 中有一个名为 .version 的文件,该文件中只有程序的版本。我想将该数字与安装程序的编号进行比较。不过,我不确定如何在 if 语句中。一个 If 中可以有多个 If?这是我到目前为止所拥有的:
$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
程序的工作方式是,如果检测到文件,安装的版本与安装程序不同(或小于),并且用户说"是"进行升级,则升级命令应运行。如果用户对升级说"否",则退出脚本。
如果程序已安装并且与安装程序的版本相同,则显示错误消息并退出。
如果尚未安装程序,请跳过重新确认安装并继续正常安装。
这可能吗?谢谢!
-
将 shebang 行添加到脚本的顶部:
#!/bin/bash
-
变量赋值在没有美元符号的情况下完成:
installerver=1.1
-
来自子外壳命令的变量赋值使用括号而不是大括号:
installedver=$(cat /var/www/html/gvsms/.version)
-
您的 if 块中有一个错误的
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
你的作业有语法错误。赋值给变量时,不要将美元符号放在$
。对于命令替换,请使用括号$(...)
.
installerver=1.1
installedver=$(cat /var/www/html/gvsms/.version)
大括号用于简单的变量替换,如 echo "You bought $qty ${item}s."
.