Bash Shell脚本错误:./ myDemo:56:语法错误:未终止的引用字符串

에이바*_*에이바 1 bash syntax-error

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

#!/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
Run Code Online (Sandbox Code Playgroud)

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

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

编辑:我修复了while语句错误,但是现在当我运行脚本时,一些东西仍然无法正常工作.

  1. 似乎在b | B)中切换语句

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

  2. 在a | A)开关语句中

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
Run Code Online (Sandbox Code Playgroud)

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

ret*_*ile 5

你错过了do第二个while.('B'案例;将其与上面的'A'案例进行比较.)

我使用gvim,它将语法突出显示shell脚本,但我认为你需要将编辑器作为一个单独的问题.


至于你的问题修改:你的逻辑被打破同时在AB情况:你需要拉逻辑备份你的,如果/当...嵌套在的if你实际上并没有做任何事情.此外,请务必引用所有文件名,以便空格不会破坏您的脚本.转义嵌套引号.我相信你需要一个-e使用的echo语句\c.

所以做一些更像是:

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"
    ;;
Run Code Online (Sandbox Code Playgroud)

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