Unix Shell - Variable substitution

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > August 2007 > Variable substitution





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Variable substitution
Dattu

2007-08-27, 7:23 am

Hi All,
I have the below requirement. I Checked in google and
few of the sites and groups and unable to find the solution.


Requirement is as follows.

===
A="XYZ"
B="ABC"
XYZ_ABC="Status_300"

I would like to construct the string XYZ_ABC using $A abd $B and print
the value of $XYZ_ABC

It's like

> echo $A_$B

XYZ_ABC

Using the above values of $A_$B

Print the value of $XYZ_ABC


Something like

> echo $ ( $A_$B) should print the value "Status_300"



Thanks in advance,
Rao

Radoulov, Dimitre

2007-08-27, 7:23 am


"Dattu" <datteswararao@gmail.com> wrote ...
[...]
> Requirement is as follows.
>
> ===
> A="XYZ"
> B="ABC"
> XYZ_ABC="Status_300"
>
> I would like to construct the string XYZ_ABC using $A abd $B and print
> the value of $XYZ_ABC
>
> It's like
>
> XYZ_ABC
>
> Using the above values of $A_$B
>
> Print the value of $XYZ_ABC

[...]

bash 3.2.25(16) $ A="XYZ"
bash 3.2.25(16) $ B="ABC"
bash 3.2.25(16) $ XYZ_ABC="Status_300"
bash 3.2.25(16) $ eval "echo \$${A}_${B}"
Status_300


Dimitre


Dattu

2007-08-27, 7:23 am

Thanks a lot for the reply ....


Any other ways to caliculate the same ?


Thanks,
Rao

Janis

2007-08-27, 7:23 am

On 27 Aug., 13:58, Dattu <datteswara...@gmail.com> wrote:
> Thanks a lot for the reply ....
>
> Any other ways to caliculate the same ?


What "the same"? Please quote context!

And what's wrong with the posted solution?

>
> Thanks,
> Rao



Dattu

2007-08-27, 7:23 am

Hi Janis,

Sorry for the confusion.

On Aug 27, 4:01 pm, Janis <janis_papanag...@hotmail.com> wrote:
> On 27 Aug., 13:58, Dattu <datteswara...@gmail.com> wrote:
>
>
[vbcol=seagreen]
> What "the same"? Please quote context!


"the same" means for the posted problem as above(Original post).

> And what's wrong with the posted solution?


Nothing worng with solution. It's working fine for me. But, I would
like to know the other kinds of solutions for the problem.

>
>
>
>
>
>
> - Show quoted text -



Radoulov, Dimitre

2007-08-27, 7:23 am


"Dattu" wrote in message ...
[...]
[...]
[vbcol=seagreen]
> But, I would
> like to know the other kinds of solutions for the problem.


zsh 4.3.2 % eval print $"${!A}_${!B}"
eval print $"${A="XYZ"}_${B="ABC"}"
Status_300


Dimitre




Janis

2007-08-27, 1:25 pm

On 27 Aug., 14:07, Dattu <datteswara...@gmail.com> wrote:
> Hi Janis,
>
> Sorry for the confusion.
>
> On Aug 27, 4:01 pm, Janis <janis_papanag...@hotmail.com> wrote:
>
>
>
>
> "the same" means for the posted problem as above(Original post).


I thought so, but since this is Usenet and not a web forum you
should always quote all necessary context.

>
>
> Nothing worng with solution. It's working fine for me. But, I would
> like to know the other kinds of solutions for the problem.


Using eval is the standard way for this kind of "meta-indirection".

Janis
[vbcol=seagreen]
>
>
>
>


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com