gpt4 book ai didi

Bash 脚本错误 : unexpected EOF while looking for matching `' '

转载 作者:行者123 更新时间:2023-11-29 09:37:45 25 4
gpt4 key购买 nike

我正在尝试运行此 shell 脚本,但出现错误:

unexpected EOF while looking for matching `''

syntax error: unexpected end of file

当我将 .asd/‘ 切换为 .asd/’ 时没有错误,但似乎什么也没有发生。该脚本的目的是告诉我/Music 中的哪些音乐文件没有相应的 .asd 文件。脚本是:

#!/bin/bash
files=`mdfind -onlyin ~/Music “kMDItemUserTags==Green” | sed -E -e 's/\.[a-zA-Z1-3]+$/.asd/‘`
for aFile in $files
do
if [ ! -e $aFile ]; then
echo $aFile;
fi
done

在此先感谢您的帮助。

最佳答案

补充Alex Ives's helpful answer :

shellcheck.net用于语法检查的 shell 脚本已被唱过很多次,而且确实如此:

如果我们将您的脚本粘贴到那里(为了便于阅读,将管道分成两行之后),我们会得到(此处省略了额外的、附带的消息,但在下面进一步列出):

Line 2:
files=`mdfind -onlyin ~/Music “kMDItemUserTags==Green” |
^-- SC1015: This is a unicode double quote. Delete and retype it.
^-- SC1015: This is a unicode double quote. Delete and retype it.

Line 3:
sed -E -e 's/\.[a-zA-Z1-3]+$/.asd/‘`
^-- SC1016: This is a unicode single quote. Delete and retype it.

如您所见,shellcheck.net会确定您的问题,并且还会提供以下指示:

Line 2:
files=`mdfind -onlyin ~/Music “kMDItemUserTags==Green” |
^-- SC2006: Use $(..) instead of legacy `..`.

Line 6:
if [ ! -e $aFile ]; then
^-- SC2086: Double quote to prevent globbing and word splitting.

Line 7:
echo $aFile;
^-- SC2086: Double quote to prevent globbing and word splitting.

此外,您不应该使用 for 枚举文件名,因为这会破坏带有嵌入空格的文件名,例如。

相反,使用:

while IFS= read -r aFile; do
# work with "$aFile"
done < <(mdfind -onlyin ~/Music ...)

William Pursell在评论中正确指出 IFS= read -r aFile 因文件名嵌入换行符而失败。
也就是说,带有嵌入式换行符 的文件名很少是现实世界中的问题;相比之下,带有嵌入 空格 的文件名是 IFS= read -r aFile 正确处理的情况。

关于Bash 脚本错误 : unexpected EOF while looking for matching `' ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38285338/

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