linux中的脚本以一些声明开头:
#!/bin/bash
如果我错了,请纠正我:这可能说明要使用哪个shell.
我还看到一些脚本说:
#!/bin/bash -ex
标志-ex的用途是什么?
#!/bin/bash -ex
<=>
#!/bin/bash
set -e -x
Run Code Online (Sandbox Code Playgroud)
Man Page(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
Run Code Online (Sandbox Code Playgroud)
更新:
顺便说一句,可以在没有脚本修改的情况下设置开关.
例如,我们有脚本t.sh:
#!/bin/bash
echo "before false"
false
echo "after false"
Run Code Online (Sandbox Code Playgroud)
并想跟踪这个脚本: bash -x t.sh
output:
+ echo 'before false'
before false
+ false
+ echo 'after false'
after false
Run Code Online (Sandbox Code Playgroud)
例如,我们想跟踪脚本并在某些命令失败时停止(在我们的例子中,它将通过命令完成false):bash -ex t.sh
output:
+ echo 'before false'
before false
+ false
Run Code Online (Sandbox Code Playgroud)