LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 09:06 AM by the dsc
Updated 08-05-2023 at 08:37 PM 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.



Related line of ideas:

Code:
logfile=/dev/shm/thisspecificscript.log

log(){
echo "${@}" >> ${logfile}
}
Maybe one can think of some verbosity toggle that would print either just to the log file or both to the log file and stdout, like maybe using "tee -a" instead of echo.
Posted in Uncategorized
Views 450 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 06:26 AM.

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