LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-06-2010, 10:04 PM   #1
Calab
Member
 
Registered: Oct 2003
Location: Calgary, AB, Canada
Distribution: CentOS, Ubuntu
Posts: 56

Rep: Reputation: 2
Question Parsing unflagged parameters in bash script


I've searched online and found many examples using getopts, but nothing that clearly explains it use, nor any examples of what I'm trying to do.

I have a script named "process". It can take from 0 to 3 different options. I'd like to be able to handle these options regardless of the order that they are entered.

Syntax:
process [-v] [-d #] [-h|-?] [string]

-v = verbose mode on
-d # = how deep to do the process, expecting a number parameter
-h or -? = show command usage
string = only process lines containing the specified string

I'd like the user to be able to enter the command freestyle. In other words:

process findme
process -d 3 findme
process findme -d 3
process -v findme -d 3
etc..

This is what I'm currently using and it does not meet my needs as it can't handle the string parameter:
Code:
verbose=0;

while getopts vhd: optname
  do
    case "$optname" in
      v)
        verbose=1
        ;;
      d)
        depth="$OPTARG"
        ;;
      h | ?)
        echo "How to use this command"
        exit
        ;;
   esac
  done
How can I process the parameters passed to my script?
 
Old 04-06-2010, 10:28 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
getopts requires your options to have a dash (-) in front of them (although you can string them together ie -abc same as -a -b -c)
and to be single items (ie not a string).
(See here for getopts explanation: http://tldp.org/LDP/abs/html/internal.html#EX33)

This would generally not prove difficult with what you have as your getopts loop processes each option and then
a simple follow up if should easily process your string.
 
Old 04-07-2010, 03:12 AM   #3
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Once you are done with processing of arguments

do this

Code:
shift $(($OPTIND - 1))
That is after the end of the while loop of optargs

Now $1, $2 are your normal argument after the flag
 
Old 04-07-2010, 03:32 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
getopts does not support having arguments before options and any associated option arguments; it supports only
Code:
command [option [option argument]] ... [argument] ...
You may have more luck with the later getopt. Alternatively it may be possible to script your way around it using (referencing and setting) getopts' $OPTIND.

For similar discussion see this LQ thread.
 
  


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
bash script - parsing optional parameters yitzle Programming 5 02-17-2008 11:16 AM
Parsing a File in a Bash Script TGWDNGHN Programming 4 12-02-2005 02:38 PM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM
bash script, parsing email addresses kepler Programming 6 01-26-2004 06:47 AM

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

All times are GMT -5. The time now is 05:16 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