Sunday, March 13, 2022

[SOLVED] while do done syntax error

Issue

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

Solution

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.



Answered By - Thomas Ayoub
Answer Checked By - Willingham (WPSolving Volunteer)