Shell script debugging-run log 1 This article mainly describes how to output debug logs in shell scripts. C/C ++ debug log In C/C ++, _ file __, _ FUNC __, _ line _ indicates which log of the current row comes fromSource codeWhich function of the row of the file. For example:
#< span style = "color: # 0000ff "> include stdio. h > /* t. C */ int main () {< br> printf (" % s: % d: % s: Debug Hello world! \ N ", _ file __, _ line __, _ FUNC _); return 0 ; }< br> /* The running result is as follows: $> make T cc t. c-o t $>. /t T. c: 6: Main: Debug Hello world! $> */ |
T. C: 6: Main: Debug Hello world! It is said that this line of information comes to the main function of the 6th line of the t. c file. This method allows you to quickly locate the problem during the debugging process. Debug logs in shell scripts Use the alias function to implement a debug log method similar to C/C ++. Save the file to/etc/mydebug
#! /Bin/bash # Mydebug # Aliases are not expanded when the shell is not interactive, unless # + Expand_aliases shell option is set using shopt Shopt - S expand_aliases Case "$1" In "Debug") Alias mydebug='Echo-N $ (caller 0 | TR "": "): $ funcname: $ lineno: Log :""'; ;; *) Alias mydebug=''; ;; Esac |
Test:
#! /Bin/bash # mydebug_test.sh source / etc / mydebug debug; # If a parameter is included in debug mode, non-debug mode without any parameters. function check_apache () {< br> echo " $ (mydebug) apache [OK] "; } function check_mysql () {< br> echo " $ (mydebug) mySQL [OK] "; } function check_all () {< br> check_apache; check_mysql; } check_all; |
Debug mode running result: $>/Bin/bash mydebug_test.sh
18: check_all: mydebug_test.sh: check_apache: 8: Log: Apache [OK]
19: check_all: mydebug_test.sh: check_mysql: 13: Log: MySQL [OK]$> Running results in non-debug mode:$>/Bin/bash mydebug_test.sh
Apache [OK]
MySQL [OK]
------------- End -------------
From: GS
-------------------------------