【问题标题】:Bash Shell Scripting Errors: ./myDemo: 56: Syntax error: Unterminated quoted stringBash Shell 脚本错误:./myDemo:56:语法错误:未终止的引号字符串
【发布时间】:2009-12-07 16:55:22
【问题描述】:

有人可以看看这段代码并找出它有什么问题吗?

#!/bin/sh
while :
do
    echo " Select one of the following options:"
    echo " d or D) Display today's date and time"
    echo " l or L) List the contents of the present working directory"
    echo " w or W) See who is logged in"
    echo " p or P) Print the present working directory"
    echo " a or A) List the contents of a specified directory"
    echo " b or B) Create a backup copy of an ordinary file"
    echo " q or Q) Quit this program"
    echo " Enter your option and hit <Enter>: \c"
    read option 
    case "$option" in
        d|D) date
             ;;
        l|L) ls $PWD
             ;;
        w|w) who
                 ;;
        p|P) pwd
             ;;
        a|A) echo "Please specify the directory and hit <Enter>: \c"
             read directory
                    if [ "$directory = "q" -o "Q" ]
                then
                    exit 0
                fi

                while [ ! -d "$directory" ]
                do
                        echo "Usage: "$directory" must be a directory."
                    echo "Re-enter the directory and hit <Enter>: \c"
                    read directory

                        if [ "$directory" = "q" -o "Q" ]
                        then    
                            exit 0

                        fi

                done
                    printf ls "$directory"

            ;;  
            b|B) echo "Please specify the ordinary file for backup and hit <Enter>: \c"
             read file
                if [ "$file" = "q" -o "Q" ]
                then
                    exit 0
                fi     

                while [ ! -f "$file" ]
                do
                    echo "Usage: \"$file\" must be an ordinary file."
                    echo "Re-enter the ordinary file for backup and hit <Enter>: \c"
                    read file
                        if [ "$file" = "q" -o "Q" ]
                        then
                            exit 0
                        fi              
                done
                    cp "$file" "$file.bkup"
                 ;;

        q|Q) exit 0
             ;;

    esac
    echo
done
exit 0

有一些我无法弄清楚的语法错误。但是我应该注意,在这个 unix 系统上 echo -e 不起作用(不要问我为什么我不知道,我没有任何权限来更改它,即使我不会被允许到)

Bash Shell 脚本错误:“./myDemo ./myDemo:第 62 行:意外标记附近的语法错误 done' ./myDemo: line 62:”[已编辑]

编辑: 我修复了 while 语句错误,但是现在当我运行 脚本有些东西仍然不是 工作正常。

  1. 好像在 b|B) switch 语句中

    cp $file $file.bkup 没有 实际上将文件复制到 file.bkup ?

  2. 在 a|A) switch 语句中

ls "$directory" 不会打印目录列表供用户查看 ?

#!/bin/bash
while $TRUE
do
        echo " Select one of the following options:"
        echo " d or D) Display today's date and time"
        echo " l or L) List the contents of the present working directory"
        echo " w or W) See who is logged in"
        echo " p or P) Print the present working directory"
        echo " a or A) List the contents of a specified directory"
        echo " b or B) Create a backup copy of an ordinary file"
        echo " q or Q) Quit this program"
        echo " Enter your option and hit <Enter>: \c"
        read option
        case "$option" in
                d|D) date
                     ;;
                l|L) ls pwd
                     ;;
                w|w) who
                     ;;
                p|P) pwd
                     ;;
                a|A) echo "Please specify the directory and hit <Enter>: \c"
                     read directory
                        if [ ! -d "$directory"  ]
                        then
                                while [ ! -d "$directory" ]
                                do
                                        echo "Usage: "$directory" must be a directory."
                                        echo "Specify the directory and hit <Enter>: \c"
                                        read directory

                                        if [ "$directory" = "q" -o "Q" ]
                                        then
                                        exit 0

                                        elif [ -d "$directory" ]
                                        then
                                                ls "$directory"

                                        else
                                        continue
                                        fi
                                done
                        fi
                        ;;
                b|B) echo "Specify the ordinary file for backup and hit <Enter>: \c"
                     read file
                        if [ ! -f "$file" ]
                         then
                                while [ ! -f "$file" ]
                                do 
                                        echo "Usage: "$file" must be an ordinary file."
                                        echo "Specify the ordinary file for backup and hit <Enter>: \c"
                                        read file
                                        if [ "$file" = "q" -o "Q" ]
then
                                        exit 0
                                        elif [ -f "$file" ]
                                        then
                                        cp $file $file.bkup
                                        fi
                                done
                        fi
                        ;;

                q|Q) exit 0
                     ;;

        esac
        echo
done
exit 0

另一件事...有没有可以用来自动解析代码的编辑器?即类似于 NetBeans 的东西?

【问题讨论】:

  • ./myDemo: 6: 语法错误:")" 意外(预期为“完成”)

标签: bash syntax-error


【解决方案1】:

您在第二个 while 之后缺少一个 do。 (“B”案例;将其与上面的“A”案例进行比较。)

我使用 gvim,它将语法高亮显示 shell 脚本,但我认为您需要将编辑器作为一个单独的问题询问。


至于您修改后的问题: 在AB 情况下,您的逻辑都被破坏了:您需要将备份逻辑从您的 if/while 嵌套中提取出来……if 实际上并没有为您做任何事情。此外,请务必引用所有文件名,以免空格破坏您的脚本。转义嵌套引号。我相信您需要在使用 \c 的 echo 语句上添加一个 -e

所以做一些类似的事情:

b|B) echo -e "Specify the ordinary file for backup and hit <Enter>: \c"
    read file
    while [ ! -f "$file" ]
    do 
        echo "Usage: \"$file\" must be an ordinary file."
        echo -e "Specify the ordinary file for backup and hit <Enter>: \c"
        read file
        if [ "$file" = "q" -o "$file" = "Q" ]
        then
            exit 0
        fi
    done
    cp "$file" "$file.bkup"
    ;;

您需要对 A 案例进行相同类型的更改。

【讨论】:

  • 感谢您的帮助。我得试试 gvim。
  • 我不知道 \c 在 Bash 中与 echo -e 一起工作。我总是这样做echo -n
  • 是的,我通常也使用-n-e 虽然让各种 \somethings 按预期工作。
  • 是的,否则我熟悉-e
【解决方案2】:

A) 目录列表部分的两个问题。

  1. -o 连词并不像你想象的那样起作用。应该是:

                if [ "$directory" = "q" -o "$directory" = "Q" ]
    
  2. 你的外部“if”需要一个“else”来处理当给定的目录确实是一个直接的目录时的情况。

B) 备份部分有同样的两个问题。修复这些,两个命令选项都会起作用。

【讨论】:

    【解决方案3】:

    您在大多数地方都引用了$file 变量,但在cp 命令中却没有。应该是:

    cp "$file" "$file.bkup"
    

    您的某些echo 命令末尾带有“\c”。我认为这是特定于 csh 的。 Bash 只会逐字地回显字符“\”和“c”。

    您的语句while $TRUE 的工作原理是变量为空或未设置。如果它被设置为某个值,它将尝试将内容作为命令执行。如果你想做那种类型的无限循环,通常在 Bash 中这样完成:

    while true
    

    其中 true 是内置的 shell。或者:

    while :
    

    其中冒号是返回 true 的无操作。当然,还有其他方法可以完成同样的事情。

    l|L) 的情况下,您可能想要这样做:

    ls
    

    ls $PWD
    

    按照您现在的方式,它将尝试列出名为“pwd”的文件的条目。

    vim 和 nano 都可以为 Bash 进行语法高亮。如果尚未在 ~/.nanorc~/.vimrc 中设置它们,您可以执行以下操作:

    对于纳米:

    nano -Y sh scriptname
    

    对于 vim:

    :syntax on
    :set filetype=sh
    

    【讨论】:

    • while $TRUEls pwd 的成功之处;我在回答中错过了这些。
    • \c 不是 csh。一些旧的(POSIX 之前的)shell 使用尾随 \c 来禁止换行,过时的 X/Open 标准包括它。在 Solaris 机器上试一试,使用 /usr/xpg4/bin/sh: echo "hello\c"。
    【解决方案4】:

    1) 你不要使用这么多的echoes..来创建一个菜单系统,你可以使用cat here-document,eg

    cat <<EOF
    -------------------------------------------------------
    Select one of the following options:
    d or D) Display today's date and time
    l or L) List the contents of the present working directory
    w or W) See who is logged in
    p or P) Print the present working directory
    a or A) List the contents of a specified directory
    b or B) Create a backup copy of an ordinary file
    q or Q) Quit this program
    --------------------------------------------------------
    EOF
    

    甚至可以这样做

    echo "-------------------------------------------------------
    Select one of the following options:
    d or D) Display today's date and time
    l or L) List the contents of the present working directory
    w or W) See who is logged in
    p or P) Print the present working directory
    a or A) List the contents of a specified directory
    b or B) Create a backup copy of an ordinary file
    q or Q) Quit this program
    --------------------------------------------------------"
    

    2) 当要求use选择一个选项时,可以使用read和-p,例如

    read -p "Enter your option and hit <Enter>: " choice
    

    3) printf 比 echo 更便携,因此您应该尽可能使用它。

    【讨论】:

    • 我按照您的指示修复了所有的 echo 语句。这是我现在的错误:./myDemo ./myDemo: 6: Syntax error: ")" unexpected (expecting "done")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    相关资源
    最近更新 更多