2013-02-28 52 views
0

我提供了一個腳本來處理兩件事,捕獲標準的out/err和返回值。我使用的進程替換:捕獲來源日誌腳本中的返回值

logger.sh

exec > >(
cat 
echo $? 
) 2>&1 

test.sh

. ./logger.sh 
ls abc 

因爲我沒有一個名爲ABC的文件,我想看看這回顯值1(來自echo $?)作爲返回碼。我正在閱讀,這將不會適用於流程替換。這可以轉換爲命名管道嗎?這會工作嗎?

運行如下:

bash ./test.sh 

回答

0

試着這樣做:

#!/bin/bash 

trap 'echo "DEBUG[$?]" | tee /tmp/log' ERR 

xxxx # a non existent command 

如果你已經有了一個trap,你可以這樣做:

#!/bin/bash 

trap 'echo "DEBUG[$?]" | tee /tmp/log; do_something_else' ERR 0 1 2 3 15 

xxxx # a non existent command 
+0

尼斯,這確實工作。 ..我可以把它放在'logger.sh'中。如果腳本test.sh中有一個陷阱呢?我不想破壞它... – jcalfee314 2013-02-28 19:50:38

+0

爲了澄清,我使用了'陷阱'....'EXIT'來捕獲錯誤和成功。另外,在exec塊關閉後陷阱進入'logger.sh'。 – jcalfee314 2013-02-28 19:55:48

+0

看到我編輯的帖子 – wam 2013-02-28 19:59:20