1827474 Members
2033 Online
109965 Solutions
New Discussion

scripting question

 
SOLVED
Go to solution
yap
Regular Advisor

scripting question

Hi,
How can I put argument as variable in script? Such as:

If first argument to a script is "Arg1". If I do

x=1
echo $($x)

Can I get "Arg1" to print out? I know this is not the correct way to do. Anyone can point out the right way? Thanks.

regards,
tenghin
19 REPLIES 19
Bernhard Mueller
Honored Contributor

Re: scripting question

Hi,

x=$1
echo $($x)

Regards,
Bernhard
Umapathy S
Honored Contributor

Re: scripting question

The arguments are passed as $n where n is 1, 2, 3...

You can capture them as $1, $2 etc. You can use them as any other variable.

echo $1

HTH,
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
yap
Regular Advisor

Re: scripting question

Hi Bernhard,
Thanks for the reply. The reason I want to make the argument as variable is because sometime the position of argument can change, may be first argument, may be second. So I don't think your suggestion can work. Do you have other suggestion ?

regards,
tenghin
marc seguin
Regular Advisor
Solution

Re: scripting question

If your command is as "./myscript arg1", then you can get the value arg1 inside your script using $1 ($2, $3, ... for 2nd, 3rd,... parameters).

echo $1
=> arg1

If $arg1 is a parameter ($arg1=value1), then you can do :
eval echo \${$1}
=> value1

marc.
Umapathy S
Honored Contributor

Re: scripting question

Tenghin,
At some point you have to identify what that incoming argument means. At that same point you can assign them to your variable of choice.

I dont find any problem here.

HTH,
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
yap
Regular Advisor

Re: scripting question

Hi Marc,
Thanks a lot. That is just what I want.

Umapathy,
Thanks for you reply also.


Thanks guys!!

regards,
tenghin
Bernhard Mueller
Honored Contributor

Re: scripting question

Hi Thenghin,

so I guess you want something like

./script -1 arg1 -2 arg2
would work exactly like
./script -2 arg2 -1 arg1

???

then you would use a case statement and use shift for each $1 to read each script argument sequentially and set variables according to whether -1 -2 etc have been found until you have gone through the whole command line.

Regards,
Bernhard

yap
Regular Advisor

Re: scripting question

Hi Bernhard,
Actually what I want is assign the second last argument to a variable, such as :

x=$#
y=`expr $x - 1`
h=`eval echo \${$y}`
echo $h


I can do "eval echo \${$y}" alone but cannot assign to variable h as above.Any idea anyone ?

regards,
tenghin
Umapathy S
Honored Contributor

Re: scripting question

Tenghin,
One way to do is to use the shift. Go on shifting till last but one.

HTH,
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
yap
Regular Advisor

Re: scripting question

Hi Umapathy,
Thanks for the suggestion! Will try that.

regards,
tenghin
Mark Grant
Honored Contributor

Re: scripting question

Specifically

x=$#
(( y = x - 1 ))
while [ $y -gt 1 ]
do
shift
(( y = y - 1 ))
done
echo $1
Never preceed any demonstration with anything more predictive than "watch this"
Bernhard Mueller
Honored Contributor

Re: scripting question

Aha,

actuall $# will give you the number of arguments, so that you can evalute $(($#-1))

Regards,
Bernhard
marc seguin
Regular Advisor

Re: scripting question

Be careful, use braces because you don't know how many parameters you'll get.

1st parameter : $1 or ${1}
12th parameter : ${12}

$12 is unknown (or, in fact, ${1} with a 2 at the end)
yap
Regular Advisor

Re: scripting question

Hi Mark,
Thanks for the script. It works but I don't what are the lines "((y=x-1))" and "((y=y-1))" for ? and why we need double brackets for these lines? Sorry if I ask dumb question, I'm a newbie in scripting :).

Bernhard,
Thanks for your suggestion. But when I do
"eval $(($#-1))" in script, it gaves error. Would you mind show me the right command line? Thanks.

Marc,
Thanks for your tips!

regards,
tenghin
curt larson_1
Honored Contributor

Re: scripting question

the short way would be to do
h=$(eval echo \${$(($#-1))})

but

if (( $# < 2 )) ;then
print "not enough args"
exit
fi

#create array of command line arguments
set -A parms $*
(( x=$#-1 )) #x=second to last argument
h=${parms[$x] #h=value of second to last arugment

is easier to understand
curt larson_1
Honored Contributor

Re: scripting question

It works but I don't what are the lines "((y=x-1))" and "((y=y-1))" for ? and why we need double brackets for these lines?

the syntax ((...)) does arthmetic evaluation

(( y=y-1)) subtracts 1 from y and assigns that value to the variable y.
actually it should be (( y=$y-1))
curt larson_1
Honored Contributor

Re: scripting question

(( x=$#-1 )) #x=second to last argument
h=${parms[$x] #h=value of second to last arugment

being the array index starts at zero the above actually gives last argument and i forgot the ending brace. so this is what works

(( x=$#-2 )) #x=second to last argument
h=${parms[$x]} #h=value of second to last arugment


yap
Regular Advisor

Re: scripting question

Hi Curt,
Thanks a lot. Your first suggestion is the easier way for my need. Also thanks for explanation on the double brackets.


Again thanks to everyone for providing me with such useful tips and explanations. I sure learn a lot of things from this thread :)

regards,
tenghin
Mark Grant
Honored Contributor

Re: scripting question

Actually curt, Within the double brackets, the $ is not required. Personally, I think it's a mistake as it makes it inconsistent with every other time you reference a variable but it saves on a bit of typing.
Never preceed any demonstration with anything more predictive than "watch this"