$# , ${#}, variable substituition question (78 Views)
Reply
Frequent Advisor
Eric Lipede
Posts: 54
Registered: ‎01-31-2008
Message 1 of 5 (78 Views)
Accepted Solution

$# , ${#}, variable substituition question

Hi there
hope you guys had a good x-mas.
I have a small question (i hope).

A snippet of my current script is...

if [ $# -ge 2 ]
then
ARG_NUMBER="$#"
echo "$"$ARG_NUMBER""

it returns -> $4

...how do i get it to return the 4th argument (based on ARG_NUMBER) rather than the number of arguments without EXPLICITlLY saying echo $4 ?

Thks


Quod sum eris
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,753
Registered: ‎03-06-2006
Message 2 of 5 (78 Views)

Re: $# , ${#}, variable substituition question

You need to use eval.
Or you can copy the parms to an array:
set -A save -- "$@"
echo ${save[4-1]}
Please use plain text.
Frequent Advisor
Eric Lipede
Posts: 54
Registered: ‎01-31-2008
Message 3 of 5 (78 Views)

Re: $# , ${#}, variable substituition question

sounds good...ive only used eval on solaris and the only reference to it on my 11.23 box is /usr/lib/builtins/eval (and thats empty) ...is that a download job ?
Quod sum eris
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,753
Registered: ‎03-06-2006
Message 4 of 5 (78 Views)

Re: $# , ${#}, variable substituition question

>is that a download job?

eval is a shell builtin. Try:
eval echo "$"$ARG_NUMBER
Please use plain text.
Frequent Advisor
Eric Lipede
Posts: 54
Registered: ‎01-31-2008
Message 5 of 5 (78 Views)

Re: $# , ${#}, variable substituition question

gotcha - thks
Quod sum eris
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation