using getopts to parse a command line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using getopts to parse a command line
# 1  
Old 04-26-2011
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 :

Code:
filename=""
port=""
optional arguments

type -t [A/B]
balances -b bal
prices -p

./test filename  port -t A -b bal


my code i have that won't parse the options is
This is what i have
Code:
#!/bin/bash
filename=""
port=""

while getopts b:t:p opt
 do
      case $opt in
          b) BAL="${OPTARG}"
             if [ "$BAL" == "" ]
             then
                print_usage $0
             else
                echo "Balances is " $BAL
             fi ;;

          t) TYPE="$OPTARG";;
          p) PRICES="$OPTARG";;
          ?) print_usage $0
             exit 1;;
      esac
done
echo "Balances:                "$BAL
echo "Type:                    "$TYPE


Any ideas

Last edited by pludi; 04-26-2011 at 02:41 PM..
# 2  
Old 04-26-2011
Is this schoolwork? There are guidelines, a mandatory template.

Did you chmod it executable?

What is your command line, output?

I never use getopt*, as I like to force simpler, more readable options, easily parsed with while case shift: -a -b whatever -c -d -e

Last edited by DGPickett; 04-26-2011 at 05:59 PM..
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

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 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

Help with getopts command

Hello All, I have shell script as below in a.ksh. #! /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... (7 Replies)
Discussion started by: tonsat
7 Replies

6. Programming

Parse parameters with getopts

Hi, this is my problem I have script with two parameters -n name and -s surname. Both have arguments and I want to know how parse these parameters with getopts. When you write ./names -n John -s White it find you all persons, which name is John White, but when you write ./names -n John ... (1 Reply)
Discussion started by: frees
1 Replies

7. 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

8. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies

9. 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

10. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies
Login or Register to Ask a Question