Bash Shell 脚本错误:./myDemo: 56: 语法错误:未终止的带引号的字符串
有人可以看一下这段代码并找出它有什么问题吗?
#!/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 语句错误,但是现在当我运行 编写一些东西仍然没有脚本 工作正常。
似乎是在b|B) switch语句中
cp $file $file.bkup 没有 实际上将文件复制到 file.bkup 吗?
在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.
It seems that in the b|B) switch statement
cp $file $file.bkup doesn't
actually copy the file to file.bkup ?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在第二个
while
之后缺少一个do
。 (“B”情况;将其与上面的“A”情况进行比较。)我使用 gvim,它将语法突出显示 shell 脚本,但我认为您需要将编辑器作为一个单独的问题来询问。
至于你修改后的问题:
在
A
和B
情况下,您的逻辑都被破坏了:您需要从 if/while 嵌套中提取备份逻辑...if 实际上并没有为你做任何事情。另外,请务必引用所有文件名,以免空格破坏脚本。摆脱嵌套的引号。我相信您需要在使用
\c
的 echo 语句上使用-e
。因此,请执行类似的操作:
您需要对
A
案例进行相同类型的更改。You are missing a
do
after the secondwhile
. (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
andB
cases: you need to pull the backup logic out of your if/while nesting... theif
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:
You'll need to do the same kind of change for the
A
case.A) 目录列表部分存在两个问题。
-o 连词的作用并不像您想象的那样。应该是:
当给定的目录确实是一个目录时,您的外部“if”需要一个“else”来处理这种情况。
B)备份部分也有同样的两个问题。修复这些问题,两个命令选项都将起作用。
Two problems in the A) directory listing section.
The -o conjunction doesn't work like you think it does. Should be:
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.
您在大多数地方都引用了
$file
变量,但在cp
命令中却没有引用。它应该是:您的某些
echo
命令末尾有“\c”。我认为这是 csh 特有的。 Bash 只会逐字地回显字符“\”和“c”。您的语句
while $TRUE
由于变量为 null 或未设置而起作用。如果它被设置为某个值,它将尝试将内容作为命令执行。如果您想要执行这种类型的无限循环,通常在 Bash 中完成,如下所示:其中 true 是 shell 内置函数。或者:
其中冒号是返回 true 的无操作。当然还有其他方法可以完成同样的事情。
在
l|L)
情况下,您可能想要执行以下任一操作:或者
按照您现在的方式,它将尝试列出名为“pwd”的文件的条目。
vim 和 nano 都可以为 Bash 进行语法高亮。如果它们尚未在
~/.nanorc
和~/.vimrc
中设置,您可以执行以下操作:对于 nano:
对于 vim:
You've quoted the
$file
variable in most places, but in thecp
command you don't. It should be: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:where true is a shell builtin. Or:
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:or
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:
For vim:
1)你不需要使用这么多的echoes..来创建菜单系统,你可以使用cat here-document,例如,
甚至这样就可以了
2)当要求使用选择一个选项时,你可以使用带有-p的read ,例如
3) printf 比 echo 更便携,因此您应该尽可能使用它。
1) you don't to use so many echoes.. to create a menu system, you can use cat here-document,eg
or even this will do
2) when asking use to choose an option, you can use read with -p, eg
3) printf is more portable than echo, therefore you should use it whenever possible.