【问题标题】:One more bash (now .bat) script另一个 bash(现在是 .bat)脚本
【发布时间】:2010-07-11 13:34:31
【问题描述】:

我需要在很多目录中转换大约12000个TIF文件,并尝试编写bash-script:

#!/bin/bash
find -name "*.tif" | while read f
do
 convert "$f" "${f%.*}.png"
 rm -f "$f"
done

为什么说:x.sh: 6: Syntax error: end of file unexpected (expecting "do") and what I should do?


非常感谢你们,伙计们,但我被骗了:应该运行它的计算机在 Windows 下工作。我不知道如何在 DOS 中处理字符串和循环,现在我的脚本如下所示:

FOR /R %i IN (*.tif) DO @ (set x=%i:tif%png) & (gm convert %i %xtif) & (erase /q /f %i)

%i - .tif 文件之一。

%x - 扩展名为 .png 的文件名

gm convert - 图形魔法实用程序,与图像魔法在 linux 上的转换类似。

【问题讨论】:

  • 在你的脚本上试试 dos2unix。
  • 我建议convert "$f" "${f%.*}.png" && rm -f "$f"。否则,如果转换失败,您将失去重试的机会:)

标签: windows image batch-file cycle


【解决方案1】:

语法看起来不错,但如果 EOL 有问题,请尝试在 do 之前添加分号以修复语法错误(或检查换行符是否实际存在/按照 ghostdog74 的建议进行编码):

find -name "*.tif" | while read f ; do # ...

请注意,查找/读取模式并不可靠。使用可以直接使用 find 的exec 能力(感谢 Philipp 的内联命令):

find -name "*.tif" -exec sh -c 'file=$0 && convert "$file" "${file%.tif}.png"' '{}' ';' -delete

【讨论】:

  • 你也可以内联简单的脚本:find -name '*.tif' -exec sh -c 'file=$0 && convert "$file" "${file%.tif}.png"' -delete
  • @Philipp: nice inline invocation ;) @pilcrow: 我同意,你需要一个换行符一个分号(不能同时使用),但值得一试,因为缺少换行符(例如,在输入内联时忘记了,或者由于编码问题)对我来说似乎是导致该语法错误的最可能原因。
猜你喜欢
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多