gpt4 book ai didi

bash - Bash Shell脚本错误: ./myDemo: 56: Syntax error: Unterminated quoted string

转载 作者:行者123 更新时间:2023-12-03 08:02:41 25 4
gpt4 key购买 nike

有人可以看一下这段代码,找出问题出在哪里吗?

#!/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行:意外 token done' ./myDemo: line 62:附近的语法错误” [编辑]

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

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

最佳答案

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

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

至于修改后的问题:
AB情况下,您的逻辑都被破坏了:您需要从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情况进行相同的更改。

关于bash - Bash Shell脚本错误: ./myDemo: 56: Syntax error: Unterminated quoted string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1861285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com