Shell script debugging-run log 2

Source: Internet
Author: User
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
-------------------------------



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.