Operating System - Linux
1752793 Members
6009 Online
108789 Solutions
New Discussion юеВ

> k=1 ; val$k=3 # how can i accomplish this?

 
SOLVED
Go to solution
eugene_6
Advisor

> k=1 ; val$k=3 # how can i accomplish this?

hello

well,

is there any way to achieve this?
------------------------------------
k=1
for i in p q r
do
ab$k=$i
done

> echo "ab1=$ab1" # ab1=p
-------------------------------------
so that i can store each values in
ab1, ab2, ab3 so on ..

or
in awk or gawk
is there any way that i can export value
in awk program?
like this?
--------------------------------------
awk '{n=1} {print n}'

>echo "n=$n" #n=1
--------------------------------------

thx..

Eugene Kim
8 REPLIES 8
Bernie Vande Griend
Respected Contributor
Solution

Re: > k=1 ; val$k=3 # how can i accomplish this?

I don't believe there is a way to set a variable name with another variable from a korn/borne shell.
However, this would work if you used an array such as:
ab[${i}]=p
Ye who thinks he has a lot to say, probably shouldn't.
Marco Paganini
Respected Contributor

Re: > k=1 ; val$k=3 # how can i accomplish this?

Hello,

Sure, there's always a trick that does it in unix. :)


k=1
for i in p q r
do
cmd=`echo ab$k=$i`
eval $cmd
done

That should solve your problem.

Cheers,
Paga
Keeping alive, until I die.
harry d brown jr
Honored Contributor

Re: > k=1 ; val$k=3 # how can i accomplish this?

"in awk or gawk is there any way that i can export value ?"

Sure :

var=`awk '{n=1} {print n}'`


Also, you can accomplish a lot more and have greater flexibility y writing your scipts in perl.

live free or die
harry
Live Free or Die
Volker Borowski
Honored Contributor

Re: > k=1 ; val$k=3 # how can i accomplish this?

Hi,

your basic problem is, that when the shell evaluates your variable, it just passed the part that usally sets variables (which is built in) and serches for commands.

If you cann effort to export the variable,
simply do it, and your script will work.
See this from Red Hat 6.1

$ a=3
$ export ab$a=4
$ echo $ab3
4
$

Hope this helps
Volker
eugene_6
Advisor

Re: > k=1 ; val$k=3 # how can i accomplish this?

wow..

i've searched all bash manual for this question .. (howto in linuxdoc and bash manual in www.gnu.org)

this is awesome..

where do i have to go besides the above two sites to find answers that you provide?
(at least, i want to how the codes you provide work..)


and .. about awk question..
what if i have 2 or more variables in awk process?
for example..
awk '$1 ~ /-/ { option=option$1}
$1 !` /-/ { argument=argument$`}'
is there any way to use variable option & argument outside of awk?

here's what i do
=====================
mystring=`echo $input |
awk 'BEGIN { RS=" " }
$1 ~ /-/ { option=option$1 }
$1 !~ /-/ { arg=arg" "$1 }
END { print option; print arg }' | sed 's/-/ /g'` #any way to store value which is visible outside awk?

option=`echo "$mystring" | sed -n '1p'`
arg=`echo "$mystring" | sed -n '2p'`
=====================
any more elegant way?
--;
thx
Eugene Kim

eugene_6
Advisor

Re: > k=1 ; val$k=3 # how can i accomplish this?


and..
here's what i figured out by myself

another way to do
ab$c=3
when c=1

---------------------
c=1
read ab$c << eof
3
eof
echo $abc
3
---------------------

:)

Eugene Kim
Volker Borowski
Honored Contributor

Re: > k=1 ; val$k=3 # how can i accomplish this?

Hi Eugene,

well "awk" is a very complex tool, I would not expect everything from the man-pages. Search amazon for awk. This one is quite OK

http://www.amazon.com/exec/obidos/ASIN/020107981X/qid=1005325986/sr=2-2/ref=sr_2_11_2/107-9238346-8792502

But from the man-pages, you might learn as well, there are system-variables like:

Built-in Variables
Gawk's built-in variables are:

ARGC The number of command line arguments (does not include options to gawk, or the program source).

ARGIND The index in ARGV of the current file being processed.

ARGV Array of command line arguments. The array is indexed from 0 to ARGC - 1. Dynamically changing the
contents of ARGV can control the files used for data.

CONVFMT The conversion format for numbers, "%.6g", by default.

ENVIRON An array containing the values of the current environment. The array is indexed by the environment
variables, each element being the value of that variable (e.g., ENVIRON["HOME"] might be
/home/arnold). Changing this array does not affect the environment seen by programs which gawk
spawns via redirection or the system() function. (This may change in a future version of gawk.)


Of which ENVIRON and ARGV might be of most interest. Esp. ARGV[1], ARGV[2] ... to access the parameters.


As far as your question concerning "bash" is required, I have to admit, I did not look up this behavior. I learned this loooooong time ago, when there was an old operating system called DOS, that has a completely diffrent behavior upon expanding wildcards (it simply does not do it!).
When I switched to UNIX, there where fundamental diffrences between a "sh" and "command.com" that I figured out more closely, than anyone new to "sh" would do today.

Have fun learning "awk" it is real fun.

So long
Volker

BTW: would be nice if you would assign some points.
eugene_6
Advisor

Re: > k=1 ; val$k=3 # how can i accomplish this?

hey..
i'd like to give 10 points to all the people who helped me.

this konquer browser gives me 404 nonfound(?) error
...even netscape doesn't work --;

i'll make sure i'll give u points when i logged on with windows..
..
:)
Thx again