![1748048961/source.sh:第133行:寻找匹配的“”时出现意外的EOF 1748048961/source.sh:第138行:语法错误:意外的文件结尾](https://linux22.com/image/184247/1748048961%2Fsource.sh%EF%BC%9A%E7%AC%AC133%E8%A1%8C%EF%BC%9A%E5%AF%BB%E6%89%BE%E5%8C%B9%E9%85%8D%E7%9A%84%E2%80%9C%E2%80%9D%E6%97%B6%E5%87%BA%E7%8E%B0%E6%84%8F%E5%A4%96%E7%9A%84EOF%201748048961%2Fsource.sh%EF%BC%9A%E7%AC%AC138%E8%A1%8C%EF%BC%9A%E8%AF%AD%E6%B3%95%E9%94%99%E8%AF%AF%EF%BC%9A%E6%84%8F%E5%A4%96%E7%9A%84%E6%96%87%E4%BB%B6%E7%BB%93%E5%B0%BE.png)
#!/bin/bash
#
#Lisa You
#
#12/1/2020
#
#Purpose of the script: The user can use the script to copy, read, rename, and delete files.
#
#Use of while loop to continue task until the user answers NO to continue the script
CHOICE=YES
while (($CHOICE = “YES”))
do
#Prompting the user to choose an option
PS3="Choose an option from the list above to complete a task: "; export PS3
select OPTION in COPY READ RENAME DELETE HELP EXIT
do
#Copy menu
if [ $OPTION = "COPY" ]; then
read -p "Enter an existing file name: " FILE
#validating file
if [ -f "$FILE" ]; then
continue
else
echo “The $FILE is not a file”
break;
fi
read -p "Enter the destination location: " DEST
#validating directory
if [ -d "$DEST" ]; then
continue;
else
echo "The $DEST is not a directory name."
break;
fi
#checking if file already exists
if ! [[ -f $DEST/$FILE ]];
then
cp "$FILE" "$DEST"
echo "--------------------------------------------------------------------------------"
echo "The task is completed successfully!"
echo "Error Code: $?"
else
echo "$FILE file already exists at this location $DEST”
read -p "Do you want to overwrite the existing file? YES or NO " OPT
if [[ $OPT == "YES" ]]
then
cp "$FILE" "$DEST"
echo "The task is completed successfully!"
echo "Error Code: $?"
else
echo "--------------------------------------------------------------------------------"
echo "The task has failed.!"
echo "Error Code: $?"
break
fi
fi
#Read menu
if [ $OPTION = "READ" ]; then
#validating file
read -p "Enter an existing file name: " FILE
if [ -f "$FILE" ]; then
cat $FILE
echo "Error Code: $?"
else
echo “The $FILE is not a file”
echo "Error Code: $?"
break;
fi
fi
#Rename menu
if [ $OPTION = "RENAME" ]; then
read -p "Which file do you want to rename? Type an existing file name: " FILE
#validating file
if [ -f "$FILE" ]; then
#Prompt for new file name
read -p "Type new file name with an absolute path. " NEWFILE
#Rename file
mv “$FILE” “$NEWFILE”
echo "--------------------------------------------------------------------------------"
echo "The task is completed successfully!"
echo "Error Code: $?"
echo "--------------------------------------------------------------------------------"
echo "The $FILE file is renamed to $NEWFILE."
echo "--------------------------------------------------------------------------------"
else
echo "The $FILE is not a file"
echo "Error Code: $?"
break;
fi
#Delete menu
if [ $OPTION = "DELETE" ]; then
read -p "Which file do you want to delete? Type an existing file name: " FILE
#validate file
if [ -f "$FILE" ]; then
#Ask for confirmation
read -p “Do you want to delete $FILE file? YES or NO” OPT
if [[ $OPT == "YES" ]]
then
#delete file
rm -f $FILE
else
echo "The $FILE is not a file"
break;
fi
fi
fi
#Help menu
if [ $OPTION = "HELP" ]; then
#List what menus do
echo "o Copy: copy a file. FIle to copy, Destinated directory"
echo "o Read: output the contents of a provided file. File to read"
echo "o Rename: rename a file. File to rename, New file name"
echo "o Delete: delete a file. File to delete"
echo "o Help: output a list of supported commands, their actions, and required parameters."
echo "o Exit: exit the script. "
break
fi
#Exit menu
if [ $OPTION = "EXIT" ]; then
exit 0
fi
done
echo "--------------------------------------------------------------------------------"
read -p "Do you want to keep running this script? YES or NO " CHOICE
done
这是我的代码,我收到以下错误:
1748048961/source.sh: line 133: unexpected EOF while looking for matching `"'
1748048961/source.sh: line 138: syntax error: unexpected end of file
我真的不知道为什么我会收到错误,因为我已经检查过代码很多次了。 :(
答案1
我注意到很多“垃圾”字符:第 12、17、47、70、83 和 98 行中有其他字符,而不是引号。
放入==
而不是=
放入while (($CHOICE = "YES"))
还有一个缺失fi
的if [ $OPTION = "COPY" ]; then