Bash Verification and Conditional Statements
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开发者_StackOverflow 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!
Add the shebang line to the top of the script:
#!/bin/bash
Variable assignment is done without the dollar sign:
installerver=1.1
Variable assignment from a subshell command uses parenthesis not braces:
installedver=$(cat /var/www/html/gvsms/.version)
There's a bad
fi
in your if block- You're missing an
else
block - You should probably test if the file exists before cat'ing it.
- You should double-quote all your variables
- You need spaces inside conditional brackets
[ … ]
and[[ … ]]
This will get your code running:
#!/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
Your assignments have syntax errors. When assigning to a variable you do not put the dollar sign $
. And for command substitution use parentheses $(...)
.
installerver=1.1
installedver=$(cat /var/www/html/gvsms/.version)
Curly braces is for simple variable substitution, as in echo "You bought $qty ${item}s."
.
精彩评论