2016-09-24 49 views
1

腳本在Linux與像一些聲明開始:使用標誌的

#!/bin/bash

糾正我,如果我錯了:這可能說要使用的外殼。

我也看到了一些腳本,說:

#!/bin/bash -ex

什麼用標誌的-EX

+1

在bash終端上,運行'help set'以獲取有關這些標誌的更多詳細信息... – anishsane

回答

5
#!/bin/bash -ex 

<=> 

#!/bin/bash 
set -e -x 

手冊頁(http://ss64.com/bash/set.html):

-e Exit immediately if a simple command exits with a non-zero status, unless 
    the command that fails is part of an until or while loop, part of an 
    if statement, part of a && or || list, or if the command's return status 
    is being inverted using !. -o errexit 

-x Print a trace of simple commands and their arguments 
    after they are expanded and before they are executed. -o xtrace 

UPDATE

順便說一句,這是可以設置開關不修改腳本。

例如,我們有劇本t.sh

#!/bin/bash 

echo "before false" 
false 
echo "after false" 

,想追查這個腳本:bash -x t.sh

output:

+ echo 'before false' 
before false 
+ false 
+ echo 'after false' 
after false 

例如我們想跟蹤腳本和停止如果某些命令失敗(在我們的情況下,它將通過命令false完成):

output:

+ echo 'before false' 
before false 
+ false 
3

這些在手冊頁的SHELL BUILTIN COMMANDS部分set下記載:

  • -e會導致猛砸爲管道盡快退出(或簡單的線條)返回一個錯誤

  • -x will case Bash to print the co執行它們之前的命令