Bash crop.sh:第3行:意外标记“('crop.sh:第3行:`foreach file(`ls*.pdf`)附近的语法错误”

Bash crop.sh:第3行:意外标记“('crop.sh:第3行:`foreach file(`ls*.pdf`)附近的语法错误”,bash,sh,unexpected-token,Bash,Sh,Unexpected Token,在下面的内容中,我编写了我的bash文件crop.sh。但是,当我在windows中运行它时,我遇到了这个错误。我使用了cygwin。我还安装了git并使用了mingw64。我搜索了很多,但无法解决这个问题 #!/bin/csh foreach file (`ls *.pdf`) pdfcrop --ini $file $file end 错误消息是: crop.sh: line 3: syntax error near unexpected token `(' crop.sh: li

在下面的内容中,我编写了我的bash文件crop.sh。但是,当我在windows中运行它时,我遇到了这个错误。我使用了cygwin。我还安装了git并使用了mingw64。我搜索了很多,但无法解决这个问题

#!/bin/csh

foreach file (`ls *.pdf`)

 pdfcrop --ini $file $file

end
错误消息是:

crop.sh: line 3: syntax error near unexpected token `('
crop.sh: line 3: `foreach file (`ls *.pdf`)'

危险,威尔·罗宾逊,这种情况并不罕见

也就是说,您可以使用globs而不是解析ls的输出,而不管您的shell是什么。我没有看到您的文件名,但我怀疑问题可能是文件名中的非标准字符

相反,请尝试以下方法:

#!/bin/csh

foreach file ( *.pdf )

  pdfcrop --ini "$file" "$file"

end
或者更好,在POSIX shell中实现:

#!/bin/sh

for file in *.pdf; do
    pdfcrop --ini "$file" "$file"
done

危险,威尔·罗宾逊,这种情况并不罕见

也就是说,您可以使用globs而不是解析ls的输出,而不管您的shell是什么。我没有看到您的文件名,但我怀疑问题可能是文件名中的非标准字符

相反,请尝试以下方法:

#!/bin/csh

foreach file ( *.pdf )

  pdfcrop --ini "$file" "$file"

end
或者更好,在POSIX shell中实现:

#!/bin/sh

for file in *.pdf; do
    pdfcrop --ini "$file" "$file"
done
您使用了bash标记并提到了bash,但您的代码是csh。不确定您是否需要bash解决方案或修复csh,但您肯定可以这样做:

#!/bin/bash

for file in *.pdf; do
   pdfcrop --ini "$file"  "$file"
done
由于通常认为csh不适合编写脚本,因此这可能是一个不错的选择。

您使用了bash标记并提到了bash,但您的代码是csh。不确定您是否需要bash解决方案或修复csh,但您肯定可以这样做:

#!/bin/bash

for file in *.pdf; do
   pdfcrop --ini "$file"  "$file"
done

由于通常认为csh不适合编写脚本,这可能是一个不错的选择。

您正在尝试使用/bin/sh执行一个C shell脚本。如果您有!/bin/csh作为shebang,这不是bash脚本。即使没有!/bin/csh,它也不是bash脚本:您正在尝试使用/bin/sh执行一个C shell脚本!/bin/csh作为shebang,这不是bash脚本。即使没有!/bin/csh,它也不是bash脚本: