【问题标题】:catch the shell script execution log through ruby通过 ruby​​ 捕获 shell 脚本执行日志
【发布时间】:2011-05-16 08:02:07
【问题描述】:

我尝试使用convert 命令将大量 pdf 文件转换为图像。我从我的文件夹中读取了所有文件,其中包含 pdf 和 html 文件,但 html 文件的扩展名为“.pdf”。我从远程服务器收到了这些文件,所以我无法检查哪些文件是 pdf 文件,哪些不是。我使用了这段代码:

%x[convert "#{source_path}" "#{destination_path}".jpg]

source_path指向html文件时,返回如下错误:

GPL Ghostscript 8.60:不可恢复 错误,退出代码 1 转换:后记 委托失败/home/20100.pdf': @ error/pdf.c/ReadPDFImage/645. convert: missing an image filename /home/test/20100-1.jpg'@ 错误/convert.c/ConvertImageCommand/2970。 成功错误:-file- 中的 /syntaxerror 操作数栈:

执行堆栈:%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)-- 当前分配方式为local 当前文件位置为 1

是否可以获得任何布尔值,或者有什么方法可以识别shell脚本是否正确执行?

【问题讨论】:

    标签: ruby shell imagemagick


    【解决方案1】:

    是的,检查$?.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
    

    【讨论】:

      【解决方案2】:

      除了tokland的解决方案,你也可以使用system,如果你不需要命令的返回值(但前提是它工作正常):

      >> system 'ls /etc/services'
      /etc/services
      => true
      >> system 'ls /etc/failfail'
      ls: cannot access /etc/failfail: No such file or directory
      => false
      

      【讨论】:

        【解决方案3】:

        这应该只将 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
        

        【讨论】:

        • 感谢@jm666。我喜欢第二个。这是完美的。而且,还有一件事,你能按照我的意愿改变第二个吗?我想用基本目录的文件名重命名文件($b.jpg)。例如,“/home/test/m.pdf”表示我需要“jpg”、“test_m.jpg”的名称。请帮帮我。
        猜你喜欢
        • 2014-10-06
        • 2020-05-31
        • 1970-01-01
        • 2021-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        • 2015-11-01
        相关资源
        最近更新 更多