$ of $i


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers $ of $i
# 1  
Old 02-10-2012
$ of $i

Is it possible to calculate $ of $i?

for example:

There is a script : double.bash
-------------
i = 1;

valueofi = $i
file1=$1
file2=$2
argument = $($i) >>>>>>? IS this possible?
--------------
I run it by: bash double.bash filename1 filename2

I was wondering if it is possible to do this operation the $($i) ?
# 2  
Old 02-10-2012
Could you explain what you're actually trying to do, instead of the syntax you made up for it? There's quite probably a way to do what you want, but I can only guess what you're actually after.

If you want to get the value of a variable given its name, you can use the ${!VAR} syntax in bash and ksh:

Code:
$ ASDF="qwerty"
$ VARNAME="ASDF"
$ echo "${!VARNAME}"
qwerty

$

# 3  
Old 02-10-2012
$1 gives the arguments while you call the file.

now replace 1 by i where i =1
=> $i => 1 but not the arguments...

=> SO I was thinking if there is something like $($i) = $(1) = argument1
# 4  
Old 02-10-2012
That's still exactly what I showed you.

Code:
$ echo $0
-bash

$ VARNAME=0
$ echo "${!VARNAME}"
-bash

$

# 5  
Old 02-10-2012
Interesting. The ${!VARNAME} syntax does not work on our ksh or ksh93 on Solaris. Throws a bad substitution error.

Doing it using eval works though with either shell:

Code:
#!/bin/bash

ASDF="qwerty"
VARNAME="ASDF"

# If ksh, below errors with:   "${!VARNAME}": bad substitution
echo ${!VARNAME}

# This works with either shell:
eval echo "\$${VARNAME}"

Output:
$ efs
qwerty
qwerty
$
# 6  
Old 02-10-2012
Thank you

Ooopss....
Thanks for both the solutions!
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question