linux中的set,linuxset

來源:互聯網
上載者:User

linux中的set,linuxset

1、set -e

"Exit immediately if a simple command exits with a non-zero status."

在“set -e”之後出現的代碼,一旦出現傳回值非零,整個指令碼就會立即退出。

2、set -o pipefail

"If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a  non-zero  status,or zero if all commands in the pipeline exit successfully.  This option is disabled by default."

在這個設定執行後,其後面的代碼,包括管道命令的傳回值,為最後一個非零的命令的傳回值,或者當管道內的所有命令都執行成功後返回零。

如下例子所示:

在沒有設定set -o pipifail時

#!/bin.bash# there is no a.test,but have b.testcat a.testecho $?cat b.testecho $?cat b.test | echo "hi"echo $?

cat a.test | echo "hi"
echo $?

執行結果如下:

linux-UMLhEm:/home/test/shell # sh -x tst.sh+ cat a.testcat: a.test: No such file or directory+ echo 11+ cat b.test----this is a test-----+ echo 00+ cat b.test+ echo hihi+ echo 00+ cat a.test+ echo hihicat: a.test: No such file or directory+ echo 00

 可以看到在執行  cat a.test | echo "hi"   時,返回的是最右邊命令執行的結果。

下面設定set -o pipeline,樣本如下:

set -o pipefailcat b.test | echo "hi"echo $?cat a.test | echo "hi"echo $?

 輸出結果如下:

+ set -o pipefail+ cat b.test+ echo hihi+ echo 141141+ cat a.test+ echo hihicat: a.test: No such file or directory+ echo 11

  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.