【问题标题】:while do done syntax errorwhile do done 语法错误
【发布时间】:2016-05-03 05:19:25
【问题描述】:

我正在编写一个脚本来帮助其他人使用 apt 包管理器。该脚本运行良好,直到我将其移至闪存驱动器并将其放在我的另一台计算机上。 我无法摆脱的错误是:

/bin/az.sh:第 57 行:意外标记“完成”附近的语法错误

/bin/az.sh:第 57 行:`完成;'

这是脚本中的代码:

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

【问题讨论】:

  • 有时问题是一个回车,在通过窗口转换时滑入......至少 bash 无法应对。
  • 我把它从linux转移到linux
  • 您在第 29 行的 if 的方括号之间缺少一个空格。此外,您似乎至少缺少一个 fi ifs
  • 非常感谢您的快速回复!解决了这个问题,现在我知道要留意那种东西了

标签: bash while-loop scripting apt


【解决方案1】:

您的第 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

你会很快发现错误。

【讨论】:

  • @BobBobson 不客气。请参阅编辑以了解如何处理此类错误。
  • 感谢我是脚本新手,所以我有很多问题。有点离题,但无论如何我可以将“apt-get install”的输出存储到文件中吗?我有“apt-get install $package -y >> /var/log/installlog”,但它仍然在窗口中打印输出,只有前几行最终出现在文件中......我还尝试了 tee 命令结果相似
猜你喜欢
  • 1970-01-01
  • 2014-10-04
  • 2014-03-02
  • 2021-12-05
  • 1970-01-01
  • 2016-01-10
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多