3

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!

2
  • I think you forgot to put the hashbang line in your script. Try inserting !#/bin/bash on the first line. Commented May 23, 2011 at 22:21
  • I did put it in there, but it was up above outside of this pasted problem area. Commented May 23, 2011 at 22:31

2 Answers 2

3
  1. Add the shebang line to the top of the script:

    #!/bin/bash
    
  2. Variable assignment is done without the dollar sign:

    installerver=1.1
    
  3. Variable assignment from a subshell command uses parenthesis not braces:

    installedver=$(cat /var/www/html/gvsms/.version)
    
  4. There's a bad fi in your if block

  5. You're missing an else block
  6. You should probably test if the file exists before cat'ing it.
  7. You should double-quote all your variables
  8. 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
Sign up to request clarification or add additional context in comments.

2 Comments

Looks awesome! Thanks! But, it still doesn't work. Installation beginning.... Checking to see if GVoice SMS Notification System was previously installed...Yes. I have detected a previous installation of GVoice SMS Notification System. ./test.x: line 15: [: missing ]' ./test.x: line 15: : command not found ` Grr. The code markup is messing with the error. Should be missing `]'
@Ross: You were also missing a space before the closing bracket.
0

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.".

5 Comments

Alright, I fixed that and removed the $ at the variable definitions. But now I get this: cat: /var/www/html/gvsms/.version: No such file or directory Installation beginning.... Checking to see if GVoice SMS Notification System was previously installed...Yes. I have detected a previous installation of GVoice SMS Notification System. ./test.x: line 13: [: 1.1: unary operator expected No. I have not detected a previous installation of GVoice SMS Notification System. To proceed with installation at your own risk, press Enter. Otherwise Ctrl-C.
/var/www/html/gvsms/.version: No such file or directory .. and you're absolutely sure that file exists? Seems like a metaphysical certainty that it is a code error or a mis-named file.Check that there are no space or other non-display char in the filename itself OR your code is somehow adding a space or other char. for Filename, use ls -l /var/www/html/gvsms/.version* | cat -vet to look for spaces, etc in the filename. Good luck!
Sorry. My bad. It does exist now. New output is: Installation beginning.... Checking to see if GVoice SMS Notification System was previously installed...Yes. I have detected a previous installation of GVoice SMS Notification System. No. I have not detected a previous installation of GVoice SMS Notification System. To proceed with installation at your own risk, press Enter. Otherwise Ctrl-C.
No, still not working. I have abandoned it for a while and stumbled across it.
To clarify: This is working for me, except that my original question was not completely answered. I still need to know how to run a different script if the user chose to upgrade or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.