You can rewrite the same program as follows
----------------------------------- #!/bin/bash
count=0 var="arg"
while test $# -ne 0 ; do count=$[ $count + 1 ] if test $cmd -eq -1 ; then shift arg1=$1 echo $arg1 fi done -----------------------------------
Couple of points.
Instead of using expr you can use bash' inbuilt expression evaluator using $[ and ]. Also use "test <condition>" instead of "`test <condition>`" again using inbuilt test.
Amitay.
On Mon, Dec 16, 2002 at 09:59:13PM +0530, Nikhil Joshi wrote:
- 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
-- _______________________________________________
Amitay.