Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-03-2009, 08:18 AM   #1
Registered: Aug 2006
Posts: 153

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


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
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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.
./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
./solar -f 'my file,my other file'

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

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

    # Test for mandatory options not set
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if [[ "$search_string" == '' -o "$files" == '' ]]; then
        <show short help message>
        \exit 1
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.


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:17 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration