1

I am working on a script that will help other people use the apt package manager. the script was working fine until i moved it to a flash drive and put it on my other computer. the error I can't get rid of is:

/bin/az.sh: line 57: syntax error near unexpected token `done'

/bin/az.sh: line 57: `done;'

here is the code from the script:

#!/bin/bash
procid=$(lsof /var/lib/dpkg/lock > /var/log/az1; sed '1d' /var/log/az1 | awk '{print $2}' )
#kill any process using apt manager 
if [ "`lsof /var/lib/dpkg/lock`" = "" ]; then
    echo "no conflicts"
else
    figlet killed old process
    kill -9 $procid 
fi;
figlet updating package lists
#apt-get update > /var/log/azupdatelog
while [ $input != "q" ]; do
    echo "(1) Find package by name"
    echo "(2) Find package by description"
    echo "(3) Install package"
    echo "(4) Remove package"
    echo "(5) Fix Dependencies"
    echo "(6) Perform an upgade"
    echo "(q) Quit "
    read input
    if [ $input = "1" ]; then 
        echo "enter package name"
        read package
        apt-cache show $package
    elif [ $input = "2" ]; then
        echo "enter search keyword"
        read package
        apt-cache search $package
    elif [ $input = "3" ]; then
        apt-get build-dep $package
        apt-get install $package -y > /var/log/installlog
        exitstatus=$(echo $?)
        if [ $exitstatus = "0" ]; then
            figlet Installation Succesful
        elif [ $exitstatus = "1" ]; then
            figlet Failure check /var/log/installlog
            figlet check now?
            echo "y or n"
            read input2
                if [ $input2 = "y"]; then
                    cat /var/log/installlog
                fi;
    elif [ $input = "4" ]; then
        echo "enter package name"
        read package
        apt-get remove $package
    elif [ $input = "5" ]; then
        echo "enter package name"
        read package
        apt-get build-dep $package
    elif [ $input = "6" ]; then
        echo ""
        apt-get upgrade
    else
        echo "oops check your input"
    fi;
done
Community
  • 1
  • 1
Bob Bobson
  • 23
  • 4
  • Sometimes the issue is a carriage return which slipped in when transitioning through windows... at least bash cannot cope with that. – Peter - Reinstate Monica Jan 26 '16 at 15:23
  • i transferred it from linux to linux – Bob Bobson Jan 26 '16 at 15:26
  • you are missing a space between the square brackets for the `if` on line 29. Also you appear to be missing at least one `fi` for you `ifs` – 123 Jan 26 '16 at 15:27
  • Thank you so much for the quick response! fixed the problem and now I know to keep an eye out for that kind of stuff – Bob Bobson Jan 26 '16 at 15:39
  • Possible duplicate of [How do I compare two string variables in an 'if' statement in Bash?](http://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash) – Benjamin W. Jan 26 '16 at 15:56

1 Answers1

2

Your line 35 elif [ $exitstatus = "1" ]; then don't have corresponding fi;

Next time you've got this kind of error, remove all noise in the current scope:

while [ $input != "q" ]; do
    if [ $input = "1" ]; then   
    elif [ $input = "2" ]; then 
    elif [ $input = "3" ]; then 
        if [ $exitstatus = "0" ]; then #There is no corresponding fi;  
        elif [ $exitstatus = "1" ]; then   
                if [ $input2 = "y"]; then
                fi;
    elif [ $input = "4" ]; then 
    elif [ $input = "5" ]; then 
    elif [ $input = "6" ]; then
    else
    fi;
done

And you'll spot the error pretty fast.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • @BobBobson You're welcome. See edit to understand how to deal with this kind of error. – Thomas Ayoub Jan 26 '16 at 15:45
  • thanks I'm new to scripting so I have alot of questions. Kind of off topic but is there anyway I can store the output of "apt-get install" into a file? I have "apt-get install $package -y >> /var/log/installlog" but it still prints the output in the window and only the first few lines end up in the file.... I also tried the tee command with similar results – Bob Bobson Jan 26 '16 at 16:46