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
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.
Hi,
You could use this too
#!/bin/bash # # e.g. sample.sh foo1 foo2 foo3 # then the following will happen : arg[0]=foo1, arg[1]=foo2 , arg[2]=foo3 #
count=0 # Counts the number of parameters
until [ -z "$1" ] # loop until all the parameters are usedup do arg[$count]=$1 # the $arg[] is like a normal array and # $1 is the first argument to the script
echo -en "Argument $count is $arg[$count]\n" # Debug Output
count=$(($count+1))
shift # shifts the argument number by one thus # now $1 is argument number two rather being argument one done
thanks Ripunjay Bararia
-----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
-- _______________________________________________
-----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
On Mon, 16 Dec 2002, Nikhil Joshi wrote:
# 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
try this one, i tried with getopts, didnt impress me.
#!/bin/bash
i=0 # counter
while test $# -ne 0; do p=$1 # getting parameter value let i=i+1 if test $p = "-${i}"; then shift eval arg$i=$1 # argno=value fi shift done
echo "arg1=$arg1, arg2=$arg2"
regards sanjeev