How to use getopt in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use getopt in shell scripting
# 1  
Old 07-15-2009
How to use getopt in shell scripting

Hi,

i need to implement a command that looks like other linux/unix commands with proper parameter validation.I saw the usage of getopt but i dont know how to use it to support both short and long options in my script.

for example , commandname -f filename -db database
commandname --help

i need to do validation for the above options to my command.Please help me in using getopt to achieve the same.

Thanks ,
Padmini
# 2  
Old 07-15-2009
It will go something like this :
Code:
while getopts "f:d:h" flag
do
#  echo $flag $OPTIND $OPTARG
   case $flag in
   f) echo $flag "FILE="$OPTARG
   ;;
   d) echo $flag "Database="$OPTARG
   ;;
   h) echo Help
   ;;
   *) echo Usage
   ;;
   esac
done


to use -db and --help you can achieve using $@ with while loop and case statement. I dont know whether it is possible using getopt(s)
# 3  
Old 07-15-2009
Hi,

I am new to shell scripting.what it means using while with $@?what is the significance of $@?
# 4  
Old 07-15-2009
$@ is the argument passed.
e.g.
cmd -a file -d database
$@ will contain "a file -d database"
# 5  
Old 07-15-2009
Use getopt option as specified below in code..

#!/bin/bash

args=`getopt abc: $*`
if test $? != 0
then
echo 'Usage: -a option_a -b option_b -c option_c'
exit 1
fi

set -- $args

for i
do
case "$i" in
-a) shift;shift; opt_a=$1;echo "flag a is set to $opt_a";;
-b) shift;opt_b=$2;echo "flag b is set to $opt_b";;
-c) shift;opt_c=$3;echo "flag c is set to $opt_c";;
esac
done


Now you can use $opt_a, $opt_b, $opt_c as value for variables...
# 6  
Old 07-15-2009
how can i use long options with -l option in getopt.Please give me an example.
# 7  
Old 07-16-2009
Thanks for the replies.Using getopts am facing a problem.for ex, if i have a command
listcert -t -k keystore -p password -l label.My optstring is tp:k:l: OPTIONS since t doesnt have any argument.
if i give the command like this listcert -t -k -p passw0rd
getopts takes -p as a argument to -k .But here i failed to give an argument to -k.It doesnt show any error for this.How can i tackle this problem?Please help me

Last edited by padmisri; 07-16-2009 at 06:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

2. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

3. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

4. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

5. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

6. Shell Programming and Scripting

Help with getopt

Hi, I want to use the getopt function to parse some arguments for a script. while getopts "i:f:r:" OPTION do case $OPTION in i) iter=$OPTARG;; f) frame=$OPTARG;; r) roi=$OPTARG;; ?) echo Usage: ...... exit 2;; esac done However, I... (5 Replies)
Discussion started by: giorgos193
5 Replies

7. Shell Programming and Scripting

getopt help

I m trying to use getopt This is my script, but it doesn't take argument in variable, Please help. set - - `getopt mscl: $*` if then echo "Exiting...." exit 2 fi for i in $* do case $i in -m) MAIL="$i"; shift;; -s) SCRIPT=$OPTARG; shift;; -c) COB=$OPTARG; shift;;... (2 Replies)
Discussion started by: darshakraut
2 Replies

8. Shell Programming and Scripting

getopt

#!/bin/sh set -- `getopt "abco:" "$@"` a= b= c= o= while : do case "$1" in -a) a=1;; -b) b=1;; -c) c=1;; -o) shift; o="$1";; --) break;; esac shift done shift # get rid of -- # rest of script... # e.g. ls -l $@ (6 Replies)
Discussion started by: Hitori
6 Replies

9. Shell Programming and Scripting

getopt help

scriptname i have made a script to perform so tasks and i managed to complete the tasks for all the options the problem i am facing is that i can run the scripts individually but i would like to make it such that it can accept multiple options and give me the appropriate output e.g.... (1 Reply)
Discussion started by: problems
1 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question