Linux shell最后试试看
有没有像最后的java try catch一样的linux bash命令? 还是Linux的shell总是继续?
try { `executeCommandWhichCanFail` mv output } catch { mv log } finally { rm tmp } 那么,有点:
 { # your 'try' block executeCommandWhichCanFail && mv output } || { # your 'catch' block mv log } rm tmp # finally: this will always happen 
 根据你的例子,看起来你正在尝试做类似于总是删除临时文件的事情,而不pipe脚本如何退出。 在Bash中做这个尝试trap内置命令来捕获EXIT信号。 
 #!/bin/bash trap 'rm tmp' EXIT if executeCommandWhichCanFail; then mv output else mv log exit 1 #Exit with failure fi exit 0 #Exit with success 
  trap的rm tmp语句总是在脚本退出时执行,所以文件“tmp”总是试图被删除。 
安装的陷阱也可以重置; 只有信号名称的陷阱调用将重置信号处理程序。
 trap EXIT 
 有关更多详细信息,请参阅bash手册页: man bash 
  mv需要两个参数,所以可能是你真的想要输出文件的内容: 
 echo `{ execCommand && cat output ; } || cat log` rm -f tmp