Linux的shell scripts 還有debug功能,你知道嗎

這一節主要介紹shell scripts腳本如何debug

root@ubuntu:/tmp# bash [-nvx] sum.sh

選項與參數:

-n :不要執行 script,僅查詢語法的問題;

-v :再執行 sccript 前,先將 scripts 的內容輸出到屏幕上;

-x :將使用到的 script 內容顯示到屏幕上,這是很有用的參數!

示例一: bash -n scripts.sh

模板練習文件:

#!/bin/bash

s=0

i=0

while [ "${i}" != "100" ]

do

i=$(($i+1))

s=$(($s+$i))

done

echo "The result of '1+2+3+...+100' is: $s"

Linux的shell scripts 還有debug功能,你知道嗎

bash -n參數使用示例

上圖中我把while變為whi,然後執行bash -n sum.sh,會檢查出語法錯誤,如果語法沒有任何錯誤,則不會有任何輸出.

示例二: bash -v scripts.sh

Linux的shell scripts 還有debug功能,你知道嗎

bash -v使用示例

bash -v功能是在執行腳本之前把腳本內容全部打印出來,然後再輸出腳本內容

示例三: bash -x scripts.sh

Linux的shell scripts 還有debug功能,你知道嗎

bash -x使用示例

bash -x scripst.sh 是將使用的內容顯示出來(記住,時使用到的script語句),這一參數很重要,也很常用

除了參數調試scripts之外,set -x [scripts] set +x也可以起到debug scripts的作用

Linux的shell scripts 還有debug功能,你知道嗎

set -x set +x使用示例

執行結果和bash -x scripts.sh是一樣的


分享到:


相關文章: