0

接下来,我编写了我的 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`)'
4

2 回答 2

2

您使用了bash标签并提及bash,但您的代码是csh. 不确定您是否需要bash解决方案或修复您的 csh,但您当然可以这样做:

#!/bin/bash

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

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

于 2018-05-25T21:01:00.897 回答
1

危险,威尔·罗宾逊。通常认为csh 被认为是有害的。

也就是说,您可以(应该!)使用 glob 而不是解析 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
于 2018-05-25T21:00:53.843 回答