Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-15-2007, 03:40 PM   #1
LQ Newbie
Registered: Oct 2005
Posts: 17

Rep: Reputation: 0
Passing command line arguments through getopts and long options

I am trying to implement getopts into my Bash script for passing command line arguments, yet I am encountering difficulty with the actual syntax and long options support. The man for getopts did not include clear examples, so most examples came from other scripts on the net, which may not have the correct syntax. In addition, several approaches exist leading to more confusion with a proper explanation. Please let know if long options are a possibility with getopts. Long options are options flags like --files or --verbose while shot options are -f or -v.

GETOPT=$(getopts -o fdp:uvh -n ${SCRIPTNAME} -- "$@")
if [ $? -ne 0 ]; then

eval set -- "$GETOPT"

while [ "$1" != "" ]; do
    case "$1" in
        -f | --files )          FLAG_FILES="true"
        -d | --directories )    FLAG_DIR="true"
        -p | --preset )          shift
        -u | --undo )            FLAG_UNDO="true"
        -v | --verbose )        VERBOSE="yes"
        -h | --help )            usage
        * )                       usage
                                exit 1

Bash return a /home/neville/bin/ line 103: getopts: -o: invalid option. Please lead me to a syntax that support long options.
Old 04-16-2007, 01:02 AM   #2
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Originally Posted by neville310
GETOPT=$(getopts -o fdp:uvh -n ${SCRIPTNAME} -- "$@")

Shouldn't that be getopt, not getopts?

As for the rest, I don't use getopt, so I don't know what it expects. Try reading the man page.

Get your getopts command correct before worrying about anything else.

Old 04-16-2007, 03:51 AM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,492

Rep: Reputation: 235Reputation: 235Reputation: 235
yes you must remember

there is getopt and getopts
they ain't the same thang!
Old 04-16-2007, 07:38 AM   #4
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
He did read the man page.

I don't see anything in the getopts man page about support for long options. Here is a script that implements long option support for bash scripts, and here is a script that shows how to implement it. Caveat emptor: I have neither tried these out, nor gone through the code at any length to see how effective they may be.

Give them a try and let us know how they work.


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
Arguments too long with ls command venusian Linux - Newbie 5 04-27-2007 06:48 PM
need some help regarding command line arguments kristam269 Linux - General 1 01-23-2007 10:40 AM
Passing a text file to the command line as arguments wimnat Linux - General 2 12-05-2005 09:09 AM
passing a list of arguments to a command hdagelic Linux - General 2 05-09-2005 10:30 AM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 03:18 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 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
Open Source Consulting | Domain Registration