虽然做了语法错误

时间:2016-01-26 15:20:36

标签: bash while-loop scripting apt

我正在开发一个脚本,可以帮助其他人使用apt包管理器。该脚本工作正常,直到我将其移动到闪存驱动器并将其放在我的另一台计算机上。 我无法摆脱的错误是:

  

/bin/az.sh:line 57:意外令牌“完成”附近的语法错误

     

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

这是脚本中的代码:

#!/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

1 个答案:

答案 0 :(得分:2)

您的第35行elif [ $exitstatus = "1" ]; then没有相应的fi;

下次出现此类错误时,请删除当前范围内的所有噪音:

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

你会很快发现错误。