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

发布于 2024-08-14 05:58:25 字数 5799 浏览 5 评论 0原文

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

#!/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 不起作用(不要问我为什么我不知道,而且我没有任何权限来更改它,即使我不被允许to)

Bash Shell 脚本错误:“./myDemo ./myDemo:第 62 行:意外标记附近出现语法错误 done' ./myDemo:第 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 的东西?

Could someone take a look at this code and find out what's wrong with it?

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

There are some syntax errors that I can't figure out. However I should note that on this unix system echo -e doesn't work (don't ask me why I don't know and I don't have any sort of permissions to change it and even if I wouldn't be allowed to)

Bash Shell Scripting Error: "./myDemo ./myDemo: line 62: syntax error near unexpected token done' ./myDemo: line 62:" [Edited]

EDIT: I fixed the while statement error, however now when I run the
script some things still aren't
working correctly.

  1. It seems that in the b|B) switch statement

    cp $file $file.bkup doesn't
    actually copy the file to file.bkup ?

  2. In the a|A) switch statement

ls "$directory" doesn't print the directory listing for the user to see
?

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

Another thing... is there an editor that I can use to auto-parse code? I.e something similar to NetBeans?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南烟 2024-08-21 05:58:25

您在第二个 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 案例进行相同类型的更改。

You are missing a do after the second while. (The 'B' case; compare that against the 'A' case above it.)

I use gvim which will syntax highlight shell scripts, but I think you need to ask about editors as a separate question.


As for your modified question:
Your logic is broken in both the A and B cases: you need to pull the backup logic out of your if/while nesting... the if isn't actually doing anything for you. Also, be sure to quote all your filenames so that spaces don't break your script. Escape your nested quotes. I believe you need a -e on the echo statements that use \c.

So do something more like:

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"
    ;;

You'll need to do the same kind of change for the A case.

没有心的人 2024-08-21 05:58:25

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

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

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

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

Two problems in the A) directory listing section.

  1. The -o conjunction doesn't work like you think it does. Should be:

                if [ "$directory" = "q" -o "$directory" = "Q" ]
    
  2. Your outer "if" needs an "else" to handle the case when the directory given really is a directory right off the bat.

The B) backup section has the same two problems. Fix those, and both command options will work.

自由如风 2024-08-21 05:58:25

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

cp "$file" "$file.bkup"

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

您的语句 while $TRUE 由于变量为 null 或未设置而起作用。如果它被设置为某个值,它将尝试将内容作为命令执行。如果您想要执行这种类型的无限循环,通常在 Bash 中完成,如下所示:

while true

其中 true 是 shell 内置函数。或者:

while :

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

l|L) 情况下,您可能想要执行以下任一操作:

ls

或者

ls $PWD

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

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

对于 nano:

nano -Y sh scriptname

对于 vim:

:syntax on
:set filetype=sh

You've quoted the $file variable in most places, but in the cp command you don't. It should be:

cp "$file" "$file.bkup"

Some of your echo commands have "\c" at the end. I think that's specific to csh. Bash will just echo the characters "\" and "c" literally.

Your statement while $TRUE works by virtue of the variable being null or unset. If it gets set to some value, it will try to execute the contents as a command. If you want to do that type of infinite loop, it's typically done in Bash like this:

while true

where true is a shell builtin. Or:

while :

where the colon is a no-op that returns true. Of course there are other ways to accomplish the same thing.

In the l|L) case you probably want to do either:

ls

or

ls $PWD

the way you have it now, it's going to try to list the entry for a file named "pwd".

Both vim and nano can do syntax highlighting for Bash. If they are not already set up in ~/.nanorc and ~/.vimrc you can do these:

for nano:

nano -Y sh scriptname

For vim:

:syntax on
:set filetype=sh
我不是你的备胎 2024-08-21 05:58:25

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

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)当要求使用选择一个选项时,你可以使用带有-p的read ,例如

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

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

1) you don't to use so many echoes.. to create a menu system, you can use 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

or even this will do

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) when asking use to choose an option, you can use read with -p, eg

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

3) printf is more portable than echo, therefore you should use it whenever possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文