LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 11-23-2010, 05:59 AM   #1
mikejreading
LQ Newbie
 
Registered: May 2009
Posts: 24

Rep: Reputation: 0
Using switches


Guys,

I want to allow my program to be able to run in DEBUG mode.

IE, I want to be able to run ./program, and I want to be able to run ./program -v to show all the debug output.

How do I go about setting up something like that?
 
Old 11-23-2010, 06:02 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Which language is your program written in?
 
Old 11-23-2010, 06:11 AM   #3
mikejreading
LQ Newbie
 
Registered: May 2009
Posts: 24

Original Poster
Rep: Reputation: 0
Writing it in Bash shell script.
 
Old 11-23-2010, 06:20 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
the -v option is commonly used to request verbose output so better use -d or -D for debug. Assuming you want verbose and your script does not take any other arguments then very simply:
Code:
if [[ $1 = -v ]]; then
   verbose=true
fi

...

# Wherever you want extra output:
if [[ $verbose = true ]]; then
    echo <whatever you want>
fi
That will work and is simple; there are several ways it could be refined.

If your script takes other options with or without arguments the the bash builtin command getopts is intended for parsing them. Here's a getopts tutorial.
 
  


Reply



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
Sound switches ON and OFF rizwan84tx Linux - Software 4 01-08-2009 08:44 AM
Sound switches ON and OFF rizwan84tx Linux - Software 3 01-07-2009 03:13 PM
context switches user777 Solaris / OpenSolaris 3 12-05-2008 06:59 PM
Routers vs Switches twantrd General 4 09-30-2004 12:03 PM
switches bardinjw Linux - Networking 1 09-21-2003 09:03 AM

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

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

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