最后在shell中编写try catch

时间:2013-03-27 10:21:43

标签: shell syntax try-catch finally

是否有像linux try catch一样的linux bash命令? 或者linux shell总是继续?

try {
   `executeCommandWhichCanFail`
   mv output
} catch {
    mv log
} finally {
    rm tmp
}

6 个答案:

答案 0 :(得分:84)

嗯,有点:

{ # your 'try' block
    executeCommandWhichCanFail &&
    mv output
} || { # your 'catch' block
    mv log
}

 rm tmp # finally: this will always happen

答案 1 :(得分:76)

根据您的示例,无论脚本如何退出,您似乎都在尝试执行类似于始终删除临时文件的操作。在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

脚本退出时始终执行rm tmp中的trap语句,因此将始终尝试删除文件“tmp”。

已安装的陷阱也可以重置;只调用信号名称调用陷阱将重置信号处理程序。

trap EXIT

有关详细信息,请参阅bash手册页:man bash

答案 2 :(得分:1)

mv有两个参数,所以你可能真的想要输出文件的内容:

echo `{ execCommand && cat output ; } || cat log`
rm -f tmp

答案 3 :(得分:1)

我使用以下语法在脚本中找到了成功:

# Try, catch, finally
(echo "try this") && (echo "and this") || echo "this is the catch statement!"

# this is the 'finally' statement
echo "finally this"

如果任何一个try语句引发错误或以exit 1结尾,那么解释器将继续执行catch语句,然后执行finally语句。

如果两个try语句都成功(和/或以exit结尾),则解释器将跳过catch语句,然后运行finally语句。

示例_1:

goodFunction1(){
  # this function works great
  echo "success1"
}

goodFunction2(){
  # this function works great
  echo "success2"
  exit
}

(goodFunction1) && (goodFunction2) || echo "Oops, that didn't work!"

echo "Now this happens!"

输出_1

success1
success2
Now this happens!

示例_2

functionThrowsErr(){
  # this function returns an error
  ech "halp meh"
}

goodFunction2(){
  # this function works great
  echo "success2"
  exit
}

(functionThrowsErr) && (goodFunction2) || echo "Oops, that didn't work!"

echo "Now this happens!"

Output_2

main.sh: line 3: ech: command not found
Oops, that didn't work!
Now this happens!

Example_3

functionThrowsErr(){
  # this function returns an error
  echo "halp meh"
  exit 1
}

goodFunction2(){
  # this function works great
  echo "success2"
}

(functionThrowsErr) && (goodFunction2) || echo "Oops, that didn't work!"

echo "Now this happens!"

Output_3

halp meh
Oops, that didn't work!
Now this happens!

请注意,功能的顺序将影响输出。如果您需要分别尝试和捕获这两个语句,请使用两个try catch语句。

(functionThrowsErr) || echo "Oops, functionThrowsErr didn't work!"
(goodFunction2) || echo "Oops, good function is bad"

echo "Now this happens!"

输出

halp meh
Oops, functionThrowsErr didn't work!
success2
Now this happens!

答案 4 :(得分:0)

另一种方法是:

set -e;  # stop on errors

mkdir -p "$HOME/tmp/whatevs"

exit_code=0

(
  set +e;
  (
    set -e;
    echo 'foo'
    echo 'bar'
    echo 'biz'
  )
  exit_code="$?"
)

rm -rf "$HOME/tmp/whatevs"

if [[ "exit_code" != '0' ]]; then
   echo 'failed';
fi 

尽管以上内容并未真正带来任何好处:

set -e;  # stop on errors

mkdir -p "$HOME/tmp/whatevs"

exit_code=0

(
    set -e;
    echo 'foo'
    echo 'bar'
    echo 'biz'
    exit 44;
    exit 43;

) || {
   exit_code="$?"  # exit code of last command which is 44
}

rm -rf "$HOME/tmp/whatevs"

if [[ "exit_code" != '0' ]]; then
   echo 'failed';
fi 

答案 5 :(得分:0)

当我添加其他选项或以其他方式更改它们时,bash脚本通常会变得很大。当bash脚本包含很多功能时,使用'trap EXIT'可能会变得很简单。

例如,考虑一个脚本调用为

dotask TASK [ARG ...]

每个TASK可能由子步骤组成,希望在这两个子步骤之间执行清理。

在这种情况下,使用子外壳来产生作用域退出陷阱(例如)很有帮助。

function subTask (
    local tempFile=$(mktemp)
    trap "rm '${tempFile}'" exit
    ...
)

但是,使用子外壳可能很棘手,因为它们无法设置父外壳的全局变量。

此外,编写单个出口陷阱通常很不方便。例如,清理步骤可能取决于函数在遇到错误之前走了多远。能够进行RAII样式的清理声明会很不错:

function subTask (
    ...
    onExit 'rm tmp.1'
    ...
    onExit 'rm tmp.2'
    ...
)

使用类似的东西似乎很明显

handlers=""
function onExit { handlers+="$1;"; trap "$handlers" exit; }

更新陷阱。但这对于嵌套子shell失败,因为这会导致父shell的处理程序过早执行。客户端代码必须在子shell的开头显式重置handlers变量。

[multiple bash traps for the same signal]中讨论的通过使用trap -p EXIT的输出修补陷阱的解决方案同样会失败:即使子外壳程序不继承EXIT陷阱,trap -p exit将显示父外壳的处理程序,因此再次需要手动重置。