【问题标题】:handling unexpected bash command output处理意外的 bash 命令输出
【发布时间】:2014-04-04 12:01:54
【问题描述】:

我正在尝试处理来自 for 循环中命令的意外输出。我正在使用专有的解压缩实用程序 (mfunzip),但是当该实用程序遇到损坏的文件时,它开始回显无休止的“InputBit 中的致命错误!”消息。

我正在尝试捕获输出并继续循环到下一个文件。

for FILE in $(ls); do
   if [ -d "$FILE" ]; then
      ...
      ...
      # without command output, it runs indefinitely
      output=$(mfunzip $currentfile $extractdir)

      # it never reaches here
      echo $output
      if [[ $output = *Fatal* ]]; then
         continue  
      fi
   fi
done

但是它似乎没有工作,因为它阻塞在 output=$(mfunzip ... ) ...我假设它无休止地输出到 $output .另一个问题似乎是“致命错误”可能不会立即出现,而是在尝试 mfunzip 文件的中途出现。

有没有更好的方法来处理这种类型的输出?

【问题讨论】:

  • 与您的问题无关,但使用 * 而不是 $(ls) 更安全。
  • Abhishek 这不是来自服务器的实际脚本,我只是快速在“some”文件夹中创建了一个 for 过程的模型以显示问题所在。 :)
  • 请发布实际重现您的问题的代码。你的模型代码很好;它可能遇到的唯一问题是mfunzip 永远不会完成。

标签: bash error-handling hp-ux


【解决方案1】:

要“处理命令的(意外或预期的)输出”,您应该使用:http://en.wikipedia.org/wiki/Expect,它是专门为这类事情设计的。

【讨论】:

    猜你喜欢
    • 2012-10-08
    • 1970-01-01
    • 2021-09-14
    • 2014-03-02
    • 2018-10-31
    • 1970-01-01
    • 2018-08-16
    • 2011-12-31
    • 2019-01-24
    相关资源
    最近更新 更多