Help with getopts command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with getopts command
# 1  
Old 11-12-2010
Help with getopts command

Hello All,
I have shell script as below in a.ksh.
Code:
#! /usr/bin/ksh
while getopts a: b: ab:f: VAR
do
case $VAR in
a) A=${OPTARG}
echo $A;;
b) B=${OPTARG}
echo $B;;
ab) AB=${OPTARG}
echo $AB ;;
f) F=${OPTARG}
echo $F ;;
esac
done

When I execute sh a.ksh -a 1 -b 2 -ab 3 -f 4 as below and displays
Code:
1
2
b

Instead of as below
Code:
1
2
3
4

As I understand a,b's are repetitive that is the reason it is not working if there is a way to make it work please let me know.

Last edited by Scott; 11-12-2010 at 05:30 PM.. Reason: Please use code tags
# 2  
Old 11-12-2010
What OS are you using? AFAIK, most don't support long options. ie -ab.
# 3  
Old 11-12-2010
Agreed with purdym.
In case of
Code:
a.ksh -a 1 -b 2 -ab 3 -f 4

b is taken as argument for option a
if we try
Code:
a.ksh -a 1 -b 2 -ab3 -f 4

output will be
Code:
1
2
b3
4

# 4  
Old 11-12-2010
Quote:
Originally Posted by purdym
What OS are you using? AFAIK, most don't support long options. ie -ab.
I am sorry I would have put OS earlier: Linux 2.6.9-67.ELsmp.
Thank you for your quick response.
# 5  
Old 11-12-2010
As a follow up, if someone knows how to make long options in KSH, I'd be interested in knowing how.
# 6  
Old 11-12-2010
I want to thank you purdym and anurag.singh for reply.
As I said it is more like repetitive option issue. For example if do as below it is working fine.
Code:
#! /usr/bin/ksh
while getopts a: b: cd:f: VAR
do
  case $VAR in
    a) A=${OPTARG}
      echo $A;;
    b) B=${OPTARG}
      echo $B;;
    c) RESET_IF_REQUIRED=1 ;;
    d) DSSERVER=${OPTARG}
      echo "3" ;;
    f) F=${OPTARG}
      echo $F ;;
  esac
done
echo "Final"

Execute
Code:
$ sh a.ksh -a 1 -b 2 -cd 3 -f 4
1
2
3
4
Final

I hope there is way to do it but I am not that good with UNIX.

Last edited by Franklin52; 11-14-2010 at 10:34 AM.. Reason: Please indent your code and use code tags
# 7  
Old 11-12-2010
You just need the dash before every option...

Code:
sh a.ksh -a 1 -b 2 -c -d 3 -f 4

Also like this:

Code:
sh a.ksh -a 1 -b 2 -a 2 -b 3 -f 4

You can definitely repeat options.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How-to enforce check on getopts command

here is my script that expects the user to run it like ./best.sh -f /tmp/log.txt more best.sh #!/bin/bash while getopts ":f:" opt; do case $opt in f) file_in="$OPTARG" ;; \?) echo "Invalid option -$OPTARG" >&2 ;; esac done uname -a SunOS mymac 5.11 11.2 sun4v... (15 Replies)
Discussion started by: mohtashims
15 Replies

2. Shell Programming and Scripting

Getopts help

Hi All, I am writing a script to pass the getopts argument to the function which I have. But it as soon as I execute the script, the argument is taking it as blank. I tried using multiple way to check but its not working. Can someone please let me know what wrong in this code. function1()... (4 Replies)
Discussion started by: sidh_arth85
4 Replies

3. Shell Programming and Scripting

Getopts how to handle missing '-' in command line args.

I'm using getopts to process command line args in a Bash script. The code looks like this: while getopts ":cfmvhs:t:" option; do case $option in c) operationMode="CHECK" ;; f) operationMode="FAST" ;; m) ... (6 Replies)
Discussion started by: gencon
6 Replies

4. Shell Programming and Scripting

bash:getopts command help

How can I say one of the options is required? can I use an if statement? let say: while getopts ":c:u:fp" opt; do case $opt in c) echo "-c was triggered, Parameter: $OPTARG" >&2;; u) echo "-u was triggered, Parameter: $OPTARG" >&2;; f) echo "-u was triggered,... (2 Replies)
Discussion started by: bashily
2 Replies

5. Shell Programming and Scripting

using getopts to parse a command line

i have the following scenario want to run the following script with manadory and optional argumnets Manadory options are : filename="" port="" optional arguments type -t balances -b bal prices -p ./test filename port -t A -b bal my code i have that won't parse the options is... (1 Reply)
Discussion started by: nano2
1 Replies

6. UNIX for Dummies Questions & Answers

getopts - command line arguments

Hi, I'm having problems with a script where I wanted every single option specified in the command line to have an argument taken with it, but for some reason only d works in the code I will be showing below. For example if I did ./thisfile -a something it would come up with "a chosen with " as... (2 Replies)
Discussion started by: IceX
2 Replies

7. HP-UX

using getopts

Is there a restriction on levels of using 'getopts' ? I have several scripts, each of which requires an option as the first parameter . If I call one prg separately it works fine, but when one prg calls another prg and passes the option on the called prg, then the called prg seems not to process... (3 Replies)
Discussion started by: vslewis
3 Replies

8. Shell Programming and Scripting

File handling, getopts command in unix

I need to create a shell script having the menu with few options such as 1. Listing 2. Change permissions 3. Modify Contents 4. Delete Files 5. Exit 1. For 1. Listing: Display a special listing of files showing their date of modification and access time (side by side) along with their... (2 Replies)
Discussion started by: bab123
2 Replies

9. Shell Programming and Scripting

Help in getopts

Hi, My script will take a input file as a parameter(which is not mandatory) and also an option. ksh my_script.sh <inputfile> The option -n I have given is no way related to the input file. Now the problem here is when i execute the script specifying the input file and the option(the way... (4 Replies)
Discussion started by: chella
4 Replies

10. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies
Login or Register to Ask a Question