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 03-27-2005, 11:12 PM   #1
eremit
LQ Newbie
 
Registered: Mar 2005
Location: Berlin
Distribution: xandros 2.04
Posts: 7

Rep: Reputation: 0
Execution options in bash


Hi there,

just wondering how I can fetch execution options like this:

sh myscript -abcdef

With getopts I am only able to fetch options like this:

sh myscript -a sdlkf -b sdfkj - c slkfjsf

Anyone that understands my problem?

eremit
 
Old 03-28-2005, 01:48 AM   #2
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
giving an opstring of "abcdef" should solve your problem right?
 
Old 03-28-2005, 05:32 AM   #3
eremit
LQ Newbie
 
Registered: Mar 2005
Location: Berlin
Distribution: xandros 2.04
Posts: 7

Original Poster
Rep: Reputation: 0
Sorry if I didn't make myself clear enough, what I would like to do is to give several options where options a, b, c, d, e, f are all different options. In case of the order of the options:

sh myscript -abcdef

will have the same output as

-sh myscript -fedcba


And last; think I didn't really understand your reply zeropash: could you please try to reformulate it?

eremit
 
Old 03-28-2005, 05:44 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
The order does not matter, from what you say, I think your options don't take arguments...

Try :
Code:
#!/bin/bash

while getopts abcde option; do
    case $option in
        a) 
            echo "option: -a is set"
        ;;  
        b) 
            echo "option: -b is set"
        ;;  
        c) 
            echo "option: -c is set"
        ;;  
        d) 
            echo "option: -d is set"
        ;;  
        e) 
            echo "option: -e is set"
        ;;
    esac
done
And execute the script with any options order you want
./script -bdeac
./script -abcde
./script -cdeba
...
 
Old 03-28-2005, 09:40 PM   #5
eremit
LQ Newbie
 
Registered: Mar 2005
Location: Berlin
Distribution: xandros 2.04
Posts: 7

Original Poster
Rep: Reputation: 0
Yepp, this is what I was looking for

Thanks

eremit
 
Old 03-28-2005, 09:58 PM   #6
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Just to throw my hat in the arena, if a flag (-a, -x, -y) requires an argument, follow it with a colon in your while loop. Example, out of -abcxyz flags, pretend '-b' and '-y' require an argument (-b foo; -y foo, etc..). The argument will be stored in $OPTARG (I believe for only one loop iteration). If an argument is recieved thats not in your argument list, your variable (in this case - $arguments) will recieve a question mark in it (and you can throw an error message from there.

Code:
while getopts ab:cxy:z arguments 2>/dev/null
do
  case $arguments in
    a) echo "-a set!";;
    b) echo "-b set with value $OPTARG";;
    c) echo "-c set!";;
    x) echo "-x set!";;
    y) echo "-y set with value $OPTARG";;
    z) echo "-z set!";;
   \?) echo "Usage: scriptname [ -a ] [ -b arg ] [ -c ] etc..."
  esac
done
 
Old 03-28-2005, 10:00 PM   #7
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Thumbs up

Quote:
Originally posted by keefaz
[B]The order does not matter, from what you say, I think your options don't take arguments...
Oops, didnt see that

Ah well, good to know for future reference anyways
 
  


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: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
How to tell where the execution is going on right now? bahadur Programming 7 04-03-2005 03:42 AM
editing bash to auto activate some options? kintaroe Linux - Newbie 1 10-02-2004 02:07 PM
Kernel 2.6.2 options question - LOCKED options ? tvojvodi Linux - General 0 02-17-2004 04:23 AM
Timed Execution Undead Programming 5 09-13-2001 02:57 AM

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

All times are GMT -5. The time now is 10:13 AM.

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