Bash 验证和条件语句
我正在为 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
程序应该工作的方式是,如果检测到文件,则安装的版本与安装程序不同(或小于),并且用户说是要升级,应运行升级命令。如果用户拒绝升级,则退出脚本。
如果程序已安装并且与安装程序的版本相同,则显示错误消息并退出。
如果该程序尚未安装,请跳过重新确认安装并继续正常安装。
这可能吗? 谢谢!
I am in the process of writing an install script for a program in bash. The first part of the install detects whether the program is already installed or not. This is detected by the presence of a file in a specified directory. That part works fine. However, I want the user to be able to upgrade to the new version if the old version exists. I thought I had the code right, but it doesn't quite work. There is a file in /var/www/html/gvsms called .version that simply has the version of the program in the file. I want to compare that number to the number of the installer. I'm not sure how, though, inside an if statement. Can there be multiple Ifs inside an If? This is what I have so far:
$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."
Right now the program errors out with:
./test.x: line 1: =1.1: command not found
./test.x: line 2: ${cat /var/www/html/gvsms/.version}: bad substitution
The way the program should work is that if the file is detected, installed version is not (or less than) the same as the installer, and the user says yes to upgrade, the upgrade commands should run. If the user says no to upgrade, then exit the script.
If the program is installed and is the same version as the installer, display error message and exit.
If the program is not already installed, skip reconfirm installation and proceed with normal install.
Is this possible?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 shebang 行添加到脚本顶部:
<前><代码>#!/bin/bash
变量赋值无需美元符号即可完成:
子 shell 命令中的变量赋值使用括号而不是大括号:
if 块中存在错误的
fi
else
块[ … ]
和[[ … ]]
这将使您的代码运行:
Add the shebang line to the top of the script:
Variable assignment is done without the dollar sign:
Variable assignment from a subshell command uses parenthesis not braces:
There's a bad
fi
in your if blockelse
block[ … ]
and[[ … ]]
This will get your code running:
您的作业有语法错误。分配给变量时,不要输入美元符号
$
。对于命令替换,请使用括号$(...)
。大括号用于简单的变量替换,如
echo "You buy $qty ${item}s."
。Your assignments have syntax errors. When assigning to a variable you do not put the dollar sign
$
. And for command substitution use parentheses$(...)
.Curly braces is for simple variable substitution, as in
echo "You bought $qty ${item}s."
.