Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Advanced KORN or BASH Shell scripting question...

56 views
Skip to first unread message

Scott Barr

unread,
Aug 16, 2001, 12:01:31 AM8/16/01
to
Hey there,
I am trying to do some funky stuff with a Korn or Bash shell script.
See snippet...

-- START --

Var_Prefix="Data_To_"
Var_Suffix="Echo"
Data_To_Echo="Hello World"

echo $($Var_Prefix$Var_Suffix)

-- END --

Now what I want to happen is for the last line echo
$($Var_Prefix$Var_Suffix) to echo to the user "Hello World" and not just the
variable name "Data_To_Echo"

Any help would be app'd.

If you do know how to do this is there anything special that needs to be
done if the variable name you try to create is an Array? Anything special
for indexing?


David Scarlett

unread,
Aug 16, 2001, 12:51:26 AM8/16/01
to
"Scott Barr" <sc...@thorin.ca> wrote in message
news:99793488...@ip195.istop.com...

> Hey there,
> I am trying to do some funky stuff with a Korn or Bash shell script.
> See snippet...
>
> -- START --
>
> Var_Prefix="Data_To_"
> Var_Suffix="Echo"
> Data_To_Echo="Hello World"
>
> echo $($Var_Prefix$Var_Suffix)
>
> -- END --

Not sure about Korn/Bash script, but in Perl, I'm fairly sure you could do
it like this:

-- START --

Var_Prefix="Data_To_"
Var_Suffix="Echo"
Data_To_Echo="Hello World"

echo ${$Var_Prefix . $Var_Suffix}

-- END --

Oh, and is the massive cross-posting really necessary?


--
David Scarlett
dsca...@optushome.com.au
http://www.listen.to/artifice/
http://members.optushome.com.au/dscarlett/

"Damn it, Kif, where's the little umbrella? That's what makes it a scotch on
the rocks!"
-Capt. Zapp Brannigann, Futurama


Harry Pehkonen

unread,
Aug 16, 2001, 2:13:56 AM8/16/01
to
"Scott Barr" <sc...@thorin.ca> writes:

> Hey there,
> I am trying to do some funky stuff with a Korn or Bash shell script.
> See snippet...
>
> -- START --
>
> Var_Prefix="Data_To_"
> Var_Suffix="Echo"
> Data_To_Echo="Hello World"
>
> echo $($Var_Prefix$Var_Suffix)
>
> -- END --
>
> Now what I want to happen is for the last line echo
> $($Var_Prefix$Var_Suffix) to echo to the user "Hello World" and not just the
> variable name "Data_To_Echo"
>
> Any help would be app'd.

This ``works'' in Bourne shell:

$ Var_Prefix=Data_To_
$ Var_Suffix=Echo
$ Data_To_Echo="Hello World"
$ { echo -n "echo $"; echo -n $Var_Prefix; echo $Var_Suffix; } > /tmp/test && . /tmp/test
Hello World

...but that's embarrassing.

Harry.
harry . . . pehkonen @ @ @ mybc . . . com

Walter Briscoe

unread,
Aug 16, 2001, 3:37:22 AM8/16/01
to
In article <ijIe7.19675$A5.5...@news1.eburwd1.vic.optushome.com.au> of
Thu, 16 Aug 2001 04:51:26 in alt.unix.wizards, David Scarlett
<dsca...@optushome.com.au> writes

>"Scott Barr" <sc...@thorin.ca> wrote in message
>news:99793488...@ip195.istop.com...
>> Hey there,
>> I am trying to do some funky stuff with a Korn or Bash shell script.
>> See snippet...
>>
>> -- START --
>>
>> Var_Prefix="Data_To_"
>> Var_Suffix="Echo"
>> Data_To_Echo="Hello World"
>>
>> echo $($Var_Prefix$Var_Suffix)
>>
>> -- END --
? Var_Prefix="Data_To_"
? Var_Suffix="Echo"
? Data_To_Echo="Hello World"
? eval echo \${$Var_Prefix$Var_Suffix}
Hello World
?

"Scott Barr" <sc...@thorin.ca> gives no reason for asking the question.
Not doing so seems unhelpful. I assume it a class exercise and there is
no shame in that. He seems not to distinguish $(program) and
${variable}. There again, my impression may be completely mistaken!


>
>Not sure about Korn/Bash script, but in Perl, I'm fairly sure you could do
>it like this:

The constraints were what?

>
>-- START --
>
>Var_Prefix="Data_To_"
>Var_Suffix="Echo"
>Data_To_Echo="Hello World"
>
>echo ${$Var_Prefix . $Var_Suffix}
>
>-- END --
>
>Oh, and is the massive cross-posting really necessary?

I agree! However, it beats multiple posts. :)
--
Walter Briscoe

Nick Bachmann

unread,
Aug 16, 2001, 8:54:31 AM8/16/01
to
[Follow-Up set to c.u.a]

[snip snippet snippings]


>
> -- START --
>
> Var_Prefix="Data_To_"


This makes the variable Var_Prefix equal to the string, not the
variable, "Data_To_"


> Var_Suffix="Echo"


Same thing as above for the text string "Echo"


> Data_To_Echo="Hello World"


Ditto


>
> echo $($Var_Prefix$Var_Suffix)


This will echo those strings which you set.


>
> -- END --

What you seem to be forgetting is that when you say ' Var="Your_Mom" ',

Bash or Corn interprets that as a string of a certain kind, and not
(keyword "not") equal to a variable. This is sealed up because of the
use of quotes. Look at this sniping of code:

--Nick's Wildly Exciting Code--
Data_to_="big blue "
Echo="yonder"
Vae_a=$Data_to_
Vae_b=$Echo
Data_to_Echo="Find the Tao of shell scripting, and you will have the
understanding."
More_Data=$Vae_a$Vae_b

echo $Vae_a
echo $Vae_b
echo $More_Data
echo $Data_to_$Echo
echo $Data_to_Echo
--End of W.E.C.--

Running this gives:

[hermie@bachmann hermie]$ sh new.sh
big blue
yonder
big blue yonder
big blue yonder
Find the Tao of shell scripting, and you will have the understanding.
[hermie@bachmann hermie]$

All expected behavior.

> Now what I want to happen is for the last line echo
$($Var_Prefix$Var_Suffix)
> to echo to the user "Hello World" and not just the variable name
> "Data_To_Echo"

I can't tell you how to do this, but I can tell you the way you are
doing it isn't supposed to, and will not, work. My advice: go get a good
shell scripting book. You need it.

--
Regards,
N
-----------------------------------
Nicholas Bachmann
nabac...@yahoo.com
"To Boldly Go Where Angels Fear To Tread"
-From the Infocom Game "Stationfall"
----------------------------------

Steve Frank

unread,
Aug 16, 2001, 10:16:29 AM8/16/01
to
Be sure to thank all of these kind people who did your homework for you.

David Masterson

unread,
Aug 16, 2001, 1:05:21 PM8/16/01
to

> -- START --

> echo $($Var_Prefix$Var_Suffix)

> -- END --

Check the documentation on "eval" in your MAN page for your shell.

--
David Masterson
Sr. R&D Engineer
Synopsys, Inc.

those who know me have no need of my name

unread,
Aug 16, 2001, 2:21:41 PM8/16/01
to
[f-u set]

<99793488...@ip195.istop.com> divulged:

> I am trying to do some funky stuff with a Korn or Bash shell script.

feh. neither advanced nor funky. worse it's a faq.

--
okay, have a sig then

Matthew Landt

unread,
Aug 16, 2001, 10:22:46 AM8/16/01
to

Use the eval statement:

eval echo \${${Var_Prefix}${Var_Suffix}}

- Matt

--
_______________________________________________________________________
Matthew Landt - AIX and HACMP Cert. Specialist - la...@austin.ibm.com
IBM High Speed Interconnect - Fibre Channel I/O Dev/Test/Support
<< Comments, views, and opinions are mine alone, not IBM's. >>

Scott Barr

unread,
Aug 17, 2001, 12:50:26 AM8/17/01
to
Sorry for the massive cross-posting... I had no idea what group was the
right one, so I sent it to all of them. :) Now I know and it won't happen
again. :)

As for the reason... I have recently taken a new job where I am responsible
for maintaining the build environment (and build scripts) for the entire
company. I have a fair amount of exp. in the area but not a lot in the way
of shell scripting. The current set of scripts are about 2 years old, had
many different people who worked on the scripts (All undocumented).
Needless to say the entire build script/environment is suffering from code
rot. :) Oh yeah, did I mention that non of it is documented? :) Oh joy!

Anyhoo, the build environment has 15 different targets all with specific
Build on/Run Regression tests on/Package on requirements. Enough of the
babble, here is the reason I wanted to know...

------

Neither Bash or Korn shell allow 2 Dim arrays.

What I now can do is define a bunch of Like named variables e.g.
Platform_??? and build the full variable name at runtime and extract the
values I am looking for.

#---------------------------------------------------------------------------
---
# This is the data matrix for Building/Running Regression & Packaging by
TARGET
#
# Platform Name Build on Regression on Package on
#---------------------------------------------------------------------------
---
set -A Platform_LINUX Linux Linux Linux
set -A Platform_LINUX_OPT Linux Linux Linux
set -A Platform_MINGW32 Linux CYGWIN_NT-4.0 Linux
set -A Platform_MINGW32_OPT Linux CYGWIN_NT-4.0 Linux
set -A Platform_NET_I386 NetBSD NetBSD Linux
set -A Platform_NET_I386_OPT NetBSD NetBSD Linux
set -A Platform_SUNOS5S4 SunOS SunOS Linux
set -A Platform_SUNOS5S4_OPT SunOS SunOS Linux
set -A Platform_VWMIPS SunOS SunOS Linux
set -A Platform_VWMIPS_OPT SunOS SunOS Linux
set -A Platform_VWPPC SunOS SunOS Linux
set -A Platform_VWPPC_OPT SunOS SunOS Linux
set -A Platform_OS9PPC SunOS CYGWIN_NT-4.0 Linux
set -A Platform_VWARM CYGWIN_NT-4.0 SunOS Linux
set -A Platform_GNLIB CYGWIN_NT-4.0 CYGWIN_NT-4.0 Linux

#################################################################
# Two variables are required for this function.
#
# $1 - String: Name of variable to get data from
# $2 - Variable: Value of variable $1 will be assigned to
# variable $2.
# $3 - Integer: If $1 is an array then $3 is the index
# into that array. Index is 0 based.
GetValueOf()
{
#If two variables then we it's a straight variable else,
# if 3 then we are dealing with an array.
# 1 or more than 3 parameters, error.
if [ ${#@} -eq 1 -o ${#@} -gt 3 ]
then
#DebugEcho "GetValueOf() Bad Function Call: GetValueOf( $@ )"
eval $2="UNKNOWN"
elif [ ${#@} -eq 2 ]
then
eval $2="\$$1"
elif [ ${#@} -eq 3 ]
then
eval $2="\${$1[$3]}"
fi
}

#Now what I can do with this is the following...

############################################################################
###
# SetupBuildEnv() - Set all Environment variables for the build
#
#
SetupBuildSEnv()
{
#Figure out what OS we are supposed to use in order to build for the
desired Platform.
GetValueOf "Platform_$Platform" Target_Build_On_HostOS

#Set the target OS
GetValueOf "Platform_$Platform" Target_Run_Regression_Tests_On_HostOS
}

#So, by way of Variable naming standards and the GetValueOf() function I
can (kinda) simulate two dimensional arrays.
# With the values of Target_xxx I can rexec to the right box and run the
correct scripts to do the build.

So there we have it.

Thank you everyone for helping me out!

"Walter Briscoe" <wbri...@ponle.demon.co.uk> wrote in message
news:nGA0mmAy...@ponle.demon.co.uk...

Tom Anderson

unread,
Aug 27, 2001, 4:44:23 AM8/27/01
to

If you really want to take this approach, you could use a master
makefile
and take advantage of make's ability to re-scan and expand variables.

Another option is to use the "eval" command.

One more option is to use the word list capabilities of csh. I know,
it's
supposed to be heresy, but there are times when the clarity & simplicity
outweigh the stigma, especially when you're the one who has to maintain
it!

Finally, there is Perl.

The point is: since you are in a position of having to re-work things
anyway, explore all your options first.

cartera

unread,
Jan 21, 2002, 9:26:19 AM1/21/02
to
It looks like your Var_Suffis="Echo" can not see "Hello World"
because it sees Data_To_Echo therefore drop the Data_To_Echo but
leave "Hello World"
0 new messages