-----Original Message----- From: linuxers-admin@mm.ilug-bom.org.in [mailto:linuxers-admin@mm.ilug-bom.org.in]On Behalf Of Nikhil Joshi Sent: Monday, December 16, 2002 9:59 PM To: ilug Subject: [ILUG-BOM] Bash script doubt
- LUG meet on 12 Jan. 2003 @ VJTI
Hi!
I'm trying to take command line args with the help of a bash script But i'm not able to achieve it Pls guide.
#!/bin/bash # i want to detect command line options # e.g. sample.sh -1 foo1 -2 foo2 -3 foo3 # then i want following : arg1=foo1, arg2=foo2 , arg3=foo3 # I tried following but arg1 takes the value $2, arg2 $3 and arg3 $3
count=0 # counter to track current command line arg
for cmd in $@ do count=`expr $count + 1` # count = count + 1 if `test $cmd = -1` then arg1=$`expr $count + 1` echo $arg1 # unfortunately it gives $2 and not foo1 fi done
An easy solution is to use getopts. use help command for that. ( help getopts ) This gives the arguments in $1, $2, $3 while $# contains the number of arguments.
Hth
===== -- KD. www.kirandivekar.cjb.net
__________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com