有人可以看看这段代码并找出它有什么问题吗?
#!/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语句错误,但现在我运行了 脚本有些东西还没有 工作正常。
似乎在b | B)切换语句中
cp $ file $ file.bkup 没有 实际上将文件复制到file.bkup?
- 中 醇>
在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
另一件事......是否有一个可以用来自动解析代码的编辑器?那类似于NetBeans的东西?
答案 0 :(得分:5)
在第二个do
之后,您错过了while
。 ('B'案例;将其与其上方的'A'案例进行比较。)
我使用gvim,它会突出显示shell脚本,但我认为你需要将编辑器作为一个单独的问题。
至于你修改过的问题:
您的逻辑在A
和B
情况下都被破坏了:您需要从if / while嵌套中提取备用逻辑... 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"
;;
您需要对A
案例进行同样的更改。
答案 1 :(得分:3)
A)目录列表部分中的两个问题。
-o连接不像您认为的那样工作。应该是:
if [ "$directory" = "q" -o "$directory" = "Q" ]
你的外部“if”需要一个“else”来处理这样一个案例,当时给出的目录确实是一个目录。
B)备份部分有两个相同的问题。修复这些,两个命令选项都可以。
答案 2 :(得分:2)
您在大多数地方引用了$file
变量,但在cp
命令中却没有。它应该是:
cp "$file" "$file.bkup"
您的一些echo
命令最后有“\ c”。我认为这是针对csh的。 Bash将直接回显字符“\”和“c”。
您的语句while $TRUE
因变量为null或未设置而起作用。如果它被设置为某个值,它将尝试将内容作为命令执行。如果你想做那种类型的无限循环,它通常用Bash完成:
while true
其中true是shell内置的。或者:
while :
冒号是一个返回true的no-op。当然还有其他方法可以完成同样的事情。
在l|L)
案例中你可能想要这样做:
ls
或
ls $PWD
现在的方式,它将尝试列出名为“pwd”的文件的条目。
vim和nano都可以为Bash进行语法高亮显示。如果尚未在~/.nanorc
和~/.vimrc
中设置它们,您可以执行以下操作:
for nano:
nano -Y sh scriptname
对于vim:
:syntax on
:set filetype=sh
答案 3 :(得分:1)
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 -p "Enter your option and hit <Enter>: " choice
3)printf比echo更便携,因此你应该尽可能使用它。