LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Simple idea for a debug verbosity tool on bash scripts

Posted 09-07-2021 at 10:06 AM by the dsc
Updated 06-28-2022 at 10:40 AM by the dsc
Tags bash, debug

I used to just have lots of "echos" around my scripts printing where it is on a loop or whatever, so I have a better idea of whats happening, often somewhat more clear than with "bash -x", even though that also may help, of course. But the odds are that I'd at some point just leave such debug echos there while the script runs on a daily basis on the background, just helping .xsession-errors to become enormous, for no good reason, since it's working and I'm not reading it. And then it can take some time to go and comment all the needless echos and printfs, paying attention if they're really just debug messages.

A simple alternative is to have either a function or perhaps an auxiliary script at $PATH that would echo what's put in front of it "debug=1" or something. Maybe it can even be somewhat tweaked to have degrees of verbosity.

Code:
#!/bin/bash

dbgecho() {
(( debug > 0 )) && echo $@
}

debug=1

dbgecho "whatever man, debugmsg"
dbgecho -e "debug\ndebug\ndebug"

debug=0

dbgecho "nah"

May be even some variable/string, I guess.

Code:
((debug > 0 )) && dbgecho="echo" || dbgecho=":"

# then you'd use it with the dollar sign, though:

$dbgecho "debug message"
But if it's the last thing the script runs, it would falsely give a non-error exit sign, if that matters. Well, it would be the case anyway with plain echo, so if one's using the exit signs it has to be less sloppily planned than something that would be affected by it.

For practical debug purposes it works almost identically except for the "$" part, but maybe one or the other requires some more CPU power, although that would be probably negligible in most cases, unless there's perhaps some full-throttle never-ending loop with lots of it to the point of making a difference, if that's possible.
Posted in Uncategorized
Views 296 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 02:56 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration