LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-03-2009, 08:18 AM   #1
bittus
Member
 
Registered: Aug 2006
Posts: 151

Rep: Reputation: 15
How to use getopts to accept multiple parameters for a single option ?


Hi,

My requirement is to accept multiple parameters using a single option. can anyone help me on this ?

For example :

Suppose a script by name 'solar'. The aim of the script is to accept multiple filenames from the command prompt and search for a string which is also passed thru the command line.

Something like:
./solar -s <string to be searched> -f <file1> <file2> <file3>
I understood that I can use getopts to accept multiple arguments. But not sure how to accept multiple parameters for a single option.

Can anyone help me on this ?
 
Old 09-04-2009, 01:38 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
getopts does not support multiple arguments to an option. A comma-separated list of values would be a single argument which you could then parse into individual values in the script.
Code:
./solar -s <string to be searched> -f <file1>,<file2>,<file3>
If the file names may contain whitespace characters then they would have to be quoted on the command line and parsed with IFS set to ",". Some error trapping techniques are also illustrated below
Code:
./solar -f 'my file,my other file'

    while getopts f:s: opt 2>/dev/null
    do
        case $opt in
            s)
                search_string="$OPTARG"
                ;;
            f )
                IFS=',' files=($OPTARG)
                ;;
            * )
                <show short help message>
                \exit 1
        esac
    done

    # Test for extra arguments
    # ~~~~~~~~~~~~~~~~~~~~~~~~
    shift $(( $OPTIND-1 ))
    if [[ $* != '' ]]; then
        <show short help message>
        \exit 1
    fi

    # Test for mandatory options not set
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if [[ "$search_string" == '' -o "$files" == '' ]]; then
        <show short help message>
        \exit 1
    fi
EDIT:
Code:
IFS=',' files=($OPTARG)
is dangerous; it leaves $IFS set to ",". See this post for an explanation.

Last edited by catkin; 09-23-2009 at 12:15 PM.
 
  


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
multiple parameters named 's' fs11 Programming 3 08-07-2009 09:58 AM
Mouse Single Click Option? PipeDreams Mandriva 3 12-06-2007 09:45 PM
How to make postfix accept mail from multiple domains Farthom Linux - Newbie 4 09-14-2006 11:13 AM
getopts multiple parameters arnulfo Programming 2 12-23-2004 05:12 AM
PHP - passing multiple parameters jacksmash Programming 1 11-25-2003 10:56 AM


All times are GMT -5. The time now is 10:06 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration