LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-15-2007, 02:40 PM   #1
neville310
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.

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

eval set -- "$GETOPT"


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


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

Rep: Reputation: 31
Quote:
Originally Posted by neville310
Code:
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, 02:51 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,282

Rep: Reputation: 172Reputation: 172
yes you must remember

there is getopt and getopts
they ain't the same thang!
 
Old 04-16-2007, 06:38 AM   #4
zaichik
Member
 
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.
 
  


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


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

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