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.
Simple idea for a debug verbosity tool on bash scripts
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.
May be even some variable/string, I guess.
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:
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.
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"
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} }
Total Comments 0