关于shell:Linux-常見Debug的方法

18次阅读

共计 922 个字符,预计需要花费 3 分钟才能阅读完成。

Debug 的办法

  • sh -x 顯示執行過程,解決大部分問題
  • set 命令設置開始 / 結束 Debug 的地位 專門針對複雜的脚本
  • 單步執行,將無關代碼注釋掉
sh -x test.sh
++ pwd
+ logfile=/home/d5118267/shell_script/omnibaseTables.log
++ date '+%F %H:%M:%S'
+ Cdate='2021-09-03 02:04:15'
+ echo ============================================
+ echo 2021-09-03 02:04:15
+ T1=/appvol/ctm/omnibase/datarefine/datastructure/sql
+ T2=/appvol/ctm/omnibase/datarefine/datamodel/sql
+ T25=/appvol/ctm/omnibase/dataconsume/sql/BIZ_dataconsume_sql
+ T3=/appvol/ctm/omnibase/dataconsume/sql/IT_dataconsume_sql
+ '[' 0 -ne 4 ']'
+ echo 'Please input enough parameters'
Please input enough parameters
+ exit 1

留神事項

  1. 加號 + 代表執行進程
  2. 加號越多,代表優先級
  3. 沒有加號的,示意標準輸出

Debug 某個一個具體範圍,一條循環的脚本 …

#!/bin/bash

#1) Execute the T1-T3 tables in GCP
#2) Create/Replace tables records

logfile="`pwd`/omnibaseTables.log"
Cdate=`date "+%F %H:%M:%S"`
echo "============================================" >> $logfile
set -x   #從這裏開始檢查
echo $Cdate >> $logfile  
set +x   #從這裏結束檢查

結果:sh t0_t1.sh
+ echo 2021-09-03 02:29:54
+ set +x
Please input enough parameters

正文完
 0