我尝试使用convert
命令将大量pdf文件转换为图像。我从我的文件夹中读取了所有文件,其中包含pdf和html文件,但html文件的扩展名为“.pdf”。我从远程服务器收到这些文件,因此无法检查哪些文件是pdf文件,哪些文件不是。我用了这段代码:
%x[convert "#{source_path}" "#{destination_path}".jpg]
当source_path
指向html文件时,会返回以下错误:
GPL Ghostscript 8.60:无法恢复 错误,退出代码1转换:Postscript 委托失败
/home/20100.pdf': @ error/pdf.c/ReadPDFImage/645. convert: missing an image filename
/ home / test / 20100-1.jpg'@ 错误/ convert.c / ConvertImageCommand / 2970。 成功错误:/ syntaxerror in -file- 操作数堆栈:执行堆栈:%interp_exit
.runexec2 --nostringval--
--nostringval-- --nostringval-- 2%stopped_push --nostringval--
--nostringval---nostringval-- false 1%stopped_push 1889 1
3%oparray_pop 1888 1 3
%oparray_pop 1872 1 3
%oparray_pop 1755 1 3
%oparray_pop --nostringval--
%errorexec_pop .runexec2
--nostringval---nostringval---nostringval-- 2%stopped_push字典堆栈:
--dict:1149/1684(ro)(G) - --dict:0/20(G) - --dict:70/200(L) - 当前的分配模式是本地的 当前文件位置为1
是否可以获取任何布尔值,或者是否有任何方法可以确定shell脚本是否正确执行?
答案 0 :(得分:3)
是,检查$?.exitstatus
是否为0。
>> %x{ls /etc/services}
=> "/etc/services\n"
>> $?.exitstatus
=> 0
>> %x{ls failfail}
ls: cannot access failfail: No such file or directory
=> ""
>> $?.exitstatus
=> 2
答案 1 :(得分:3)
除了tokland的解决方案之外,如果您不需要命令的返回值(但只有在它正常工作的情况下),您还可以使用system
:
>> system 'ls /etc/services'
/etc/services
=> true
>> system 'ls /etc/failfail'
ls: cannot access /etc/failfail: No such file or directory
=> false
答案 2 :(得分:1)
这应该只将PDF文件转换为jpg。 (新文件将有扩展名file.pdf.jpg)
ls -1 | xargs file | grep ': PDF document,' | sed 's/:.*//' | xargs -I % convert % %.jpg
或者这只会将pdf文件转换为filename.jpg
ls -1 | xargs file | grep ': PDF document,' | sed 's/:.*//' | while read file; do b=`basename $file .pdf`; convert $file $b.jpg; done