Level1 15 func3 call.sh # 一層間接調(diào)用
Level2 18 main call.sh # 二層間接調(diào)用
Level3 # 無輸出 因為沒有第三層調(diào)用
局部調(diào)試
可以通過set命令構(gòu)造局部調(diào)試塊,我們可以按照如下方式添加局部調(diào)試:
set -x date set x
> bash script1.sh # 不需要添加調(diào)試參數(shù) The script starts now. date Fri 28 Feb 2020 06:23:04 PM CST set x This is a string: black And this is a number: 9
調(diào)試參數(shù)表
set -f
set -o noglob
對文件名停用元字符匹配
set -v
set -o verbose
打印輸入的命令
set -x
set -o xtrace
命令行首打印 ,執(zhí)行出錯會打印詳細信息
調(diào)試用的參數(shù)可以在運行中動態(tài)疊加或刪除:
> set -v > date date Fri 28 Feb 2020 06:54:47 PM CST > set -x # 參數(shù)可以累加 date # -v 的效果 date # -x 的效果 Fri 28 Feb 2020 06:55:37 PM CST > set vx # 取消參數(shù) set vx
通過使用-f選項可以顯著減少腳本中的轉(zhuǎn)義字符:
> ls ? x86_64-pc-linux-gnu-library > set -f # 停用元字符匹配 > ls ? ls: cannot access \\\'?\\\': No such file or directory > touch ? > ls ? \\\'?\\\' > rm ? > set f -x # 選項 x 還可以用于顯示詳細錯誤信息 > aaa aaa \\\'[\\\' -x /usr/lib/command-not-found \\\']\\\' /usr/lib/command-not-found -- aaa Command \\\'aaa\\\' not found, did you mean: command \\\'aha\\\' from deb aha (0.5-1) command \\\'jaaa\\\' from deb jaaa (0.8.4-4) command \\\'aa\\\' from deb astronomical-almanac (5.6-6) Try: sudo apt install <deb name> return 127
默認調(diào)試
也可以直接在腳本第一行添加參數(shù)讓腳本默認以調(diào)試模式啟動:
#!/bin/bash -xv
還可以在可能出錯的命令前用echo輸出調(diào)試信息:
echo debug message: now attempting to start w command; w # 用 ; 對要執(zhí)行的命令排序 echo Variable VARNAME is now set to $VARNAME.
設(shè)置選項輔助調(diào)試
為了方便調(diào)試,我們可以使用set命令對bash的選項進行設(shè)置:
> set -o # 查看所有選項的開關(guān)狀態(tài) > set -o | grep xtrace xtrace off > set -x # 等價于 set -o xtrace > set -o | grep xtrace grep --color=auto xtrace set -o xtrace on > set x # 等價于 set o xtrace set x > set -o | grep xtrace xtrace off
常用調(diào)試選項
引用為定義變量時報錯:
> unset $VAR;echo $VAR > set -u # 等價于 set -o nounset > echo $var bash: var: unbound variable
為防止誤操作覆蓋文件中的數(shù)據(jù), 設(shè)置禁止重定向到已經(jīng)存在的文件:
> set -C # 等價于 set -o noclobber > touch test > date > test bash: test: cannot overwrite existing file
設(shè)置不解析通配符:
> set -f # 等價于 set -o noglob > touch * > ll * -rw-rw-r-- 1 remilia remilia 0 Mar 1 20:09 \\\'*\\\'
到此這篇關(guān)于詳解bash中的腳本調(diào)試機制的文章就介紹到這了,更多相關(guān)bash 腳本調(diào)試 內(nèi)容請搜索西部數(shù)碼以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持西部數(shù)碼!
更多關(guān)于云服務(wù)器,域名注冊,虛擬主機的問題,請訪問西部數(shù)碼官網(wǎng):www.ps-sw.cn