短命令 | 長命令 | 效果 |
---|---|---|
set -f | set -o noglob | 對文件名停用元字符匹配 |
set -v | set -o verbose | 打印輸入的命令 |
set -x | set -o xtrace | 命令行首打印+,執(zhí)行出錯會打印詳細(xì)信息 |
調(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 還可以用于顯示詳細(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
默認(rèn)調(diào)試
也可以直接在腳本第一行添加參數(shù)讓腳本默認(rèn)以調(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的選項進(jìn)行設(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)試機(jī)制的文章就介紹到這了,更多相關(guān)bash 腳本調(diào)試 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:廊坊 德州 紅河 廣安 滁州 湛江 回訪 巴彥淖爾
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解bash中的腳本調(diào)試機(jī)制》,本文關(guān)鍵詞 詳解,bash,中的,腳本,調(diào)試,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。上一篇:最好懂的HTTPS講解