Visit Our UNIX and Linux User Community


Get Options and Parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get Options and Parameters
# 1  
Old 10-10-2009
Get Options and Parameters

Hi there.

Could someone explain how do i get the options separated from the arguments. My problem is that i have a lot of options and in all of those options i need to have the parameters available to use in that option and all the others. How can i take the arguments before i cycle throw the options?

Thanks in advance.
# 2  
Old 10-10-2009
Hi.

Perhaps you're looking for getopts?
Code:
man getopts

(it's a built-in feature, so search for getopts)

Example
bash code:
  1. while getopts a:bc: ARG; do
  2.   case "$ARG" in
  3.     a) echo Option A with $OPTARG;;
  4.     b) echo Option B;;
  5.     c) echo Option C with $OPTARG;;
  6.   esac
  7. done

Code:
./Test -a argument_a -b -c argument_c
 
Option A with argument_a
Option B
Option C with argument_c

A colon after the a and c (getopts a:bc:) means that the option takes an argument.

If your arguments have more than one word, put the words in quotes.

Code:
./Test -a "argument a" -b -c "argument c"
 
Option A with argument a
Option B
Option C with argument c

# 3  
Old 10-10-2009
That is a good example thank you.
But that's not the example that I have. I have something like this


MyScript.sh -sd -r -e Dir1/Dir2 Dir3/Dir4

This exemplifies that MyScript receives one or more options and one or more directories (the arguments for the script are folders where I need to look for files according to the options).
First I have the options and after that I have the arguments. But after I run the first option I have to have all the arguments already available in a variable or something. Is there a way to accomplish this without the getops? I know i have to use shift.

Last edited by KitFisto; 10-10-2009 at 10:25 PM..
# 4  
Old 10-10-2009
bash code:
  1. #set -A ARGS $( echo "$@" | sed "s/.*-[a-z]\+ //")
  2. ARGS=( $( echo "$@" | sed "s/.*-[a-z]\+ //") )
  3.  
  4. echo "Args: ${ARGS[*]}"
  5. echo "Arg 1: ${ARGS[0]}"
  6. echo "Arg 2: ${ARGS[1]}"
  7.  
  8. while test "$1"; do
  9.   case "$1" in
  10.     -sd) echo Option sd;;
  11.     -r) echo Option r;;
  12.     -e) echo Option e;;
  13.     *) break;;
  14.   esac
  15.   shift
  16. done
  17.  
  18.  
  19. ./Test -sd -r -e Dir1/Dir2 Dir3/Dir4
  20. Args: Dir1/Dir2 Dir3/Dir4
  21. Arg 1: Dir1/Dir2
  22. Arg 2: Dir3/Dir4
  23. Option sd
  24. Option r
  25. Option e
# 5  
Old 10-10-2009
Wow cool man. Thanks a lot. You have helped a lot. Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #822
Difficulty: Medium
In HTML5, both the strike and div elements were deprecated.
True or False?

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh processing options and parameters

I have a requirement where I need to process both options and parameters. command line call ie xxx.ksh -sid TEST1 -search_str LOCKED user1 user2 ..... I am using the following peice of code but I am usure how I can loop through all my parameters user1, user2, ... Note at the minium... (2 Replies)
Discussion started by: BeefStu
2 Replies

2. Ubuntu

Kernel boot options removed by fault, no boot options

Hello Everyone, First of all, I highly appreciate all Linux forum members and whole Linux community. http://forums.linuxmint.com/images/smilies/icon_wink.gif. I wish you the best for all of you ! I will try to be short and concise: I am using Linux Mint 10 for 2 months on 2 ws, and all went... (3 Replies)
Discussion started by: cdt
3 Replies

3. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

4. AIX

no options

Hi All, I have a situation here that's very fun... I have a system with AIX and iPlanet (sunOne) installed, when occurs an unknown event on the network the WebServer shows a thousand of CLOSE_WAIT connections and this number grows and grows until the webserver crashs. I read some documents... (2 Replies)
Discussion started by: nascimento.rp
2 Replies

5. Post Here to Contact Site Administrators and Moderators

Advt Options ?

Hi, I was wondering if you accept advertising on your site ? Regards, (1 Reply)
Discussion started by: systemali
1 Replies

6. Shell Programming and Scripting

getopts takes options for parameters

Here is my post with a question about getopts. I am running korn shell on Solaris 5.8. I am trying to ensure that certain options require a parameter, which is easy enough. I have found that if multiple options are entered on the command line together, but the parameter for one of the options is... (1 Reply)
Discussion started by: UCD-Randy
1 Replies

7. UNIX for Dummies Questions & Answers

options

I am just beginning to learn unix and I was wondering if there was a list of all the options somewhere on the net or hidden in the man pages? Also do options always have - and then a letter, or can it be - and a number as well? Thanks! (1 Reply)
Discussion started by: terms5
1 Replies

8. UNIX for Dummies Questions & Answers

cp options

Hello again, Is there an option for the cp command to overwrite existing files in the destination directory? Cheers Rob (3 Replies)
Discussion started by: milage
3 Replies

Featured Tech Videos