2013-03-04 69 views
3

有沒有一種方法可以通過xtrace隱藏命令(set -o xtrace)?從xtrace隱藏命令

在DOS中,該技術將轉爲echo on,但以@符號爲前綴隱藏的命令。

回答

4

我從來沒有見過在bash類似的伎倆,但你有時可以使用子shell來臨時啓用跟蹤命令你感興趣的:

echo "This command is not traced" 
(
    set -x 
    echo "This command is traced" 
) 
echo "No longer traced" 

如果因爲要修改子shell不適合環境,你可以set -x然後set +x,但是這會留下跟蹤set +x命令的文物。

0

你可以做這樣的事情

# At start of script, or where xtrace may be enabled: 
[[ "$SHELLOPTS" =~ xtrace ]] && xtrace_on=1 

# Later 
set +o xtrace 
# Your untraced code here 
[[ "$xtrace_on" ]] && set -o xtrace