LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2006, 06:56 PM   #1
rbautch
LQ Newbie
 
Registered: Jun 2006
Posts: 13

Rep: Reputation: 0
Shell script debugging


I'd like to add some echo statements throughout my script for dubugging purposes, but I'd like to be able so suppress them with a command line argument. I don't want the full -x dubug, just the echoes. My initial thought was to replace all the echoes with a function that gets defined using a command line argument. Is there a more elegant way?
 
Old 07-19-2006, 07:16 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I'm a big fan of:
Code:
[ $DBG_MODE ] && echo "Debug message"

DBG_MODE=1 ./script
 
Old 07-20-2006, 06:23 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
This is my standard way:

Code:
debug_out=/dev/stderr
#debug_out=/dev/null

...

echo "my debug statement" > $debug_out
This way I can have my debug statements in the console, to null, or to a file at will.

jlinkels
 
Old 07-20-2006, 06:37 AM   #4
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
set -o verbose
# Echoes all commands before executing
set +o verbose

is also handy
 
Old 07-20-2006, 08:42 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
this is the thing i always do when making script files:

Code:
message() {
   [ "$MESSAGE" ] && echo "$@"
}

debug() {
   [ "$DEBUG" ] && echo "$@"
}

error() {
   [ "$ERROR" ] && echo "$@"
}

warning() {
   [ "$WARNING" ] && echo "$@"
}

MESSAGE=1
message "your message" "anything"

DEBUG=1
debug "any"

ERROR=1
error "any"

WARNING=1
warning "any"

Last edited by konsolebox; 07-22-2006 at 12:21 AM.
 
Old 07-20-2006, 08:51 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
alias BUG=echo  # turn it on
alias BUG=:     # turn it off
# then:
BUG hello there
edit:
or alias DEBUG=\#

Last edited by bigearsbilly; 07-20-2006 at 08:53 AM.
 
Old 07-20-2006, 09:18 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
darn. for almost a year of exploring bash i never thought of that kind of code. wow you saved me a lot of statements. thanks a lot billy.
 
Old 07-20-2006, 09:21 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Simple is cool!

I've been at this game for a LONG time.
still learning though
 
Old 07-20-2006, 09:37 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
oh i forgot. the reason i wasn't using alias is because alias won't accept parameters. example alias DEBUG="2>&1 echo anything". and practically it's more easier to modify a function than an alias. so when you do:

Code:
message() {
   echo "$@" #this is for stdout
   #but what if i'll add for the logfile
   echo "$@" >> debug.log
}
anyway it depends on the usage.
aliases are simpler. functions are more flexible.
at least we have choices hehehe
thanks again billy for the info
 
Old 07-20-2006, 09:52 AM   #10
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
That is actually a really cool trick. I'd probably add >/dev/stderr on the end, but that's just me.
 
Old 07-20-2006, 10:30 PM   #11
rbautch
LQ Newbie
 
Registered: Jun 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Wow, thanks for all the great responses guys. I'll give a few of these a try.
 
Old 07-20-2006, 10:33 PM   #12
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
No problem. Let us know which one you find works best for you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Debugging shell scripts konstantinmiller Programming 9 06-08-2006 05:33 AM
Debugging a Bash Shell Script solarblast Linux - General 1 02-23-2006 01:44 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
debugging configure script Avatar33 Programming 3 03-23-2005 04:35 PM
Backup Script Tweaking and Debugging KungFuHamster Programming 1 12-07-2003 02:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
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