variable part of variable name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable part of variable name
# 1  
Old 01-25-2008
variable part of variable name

Greetings

I am having a hard time trying to figure out how to use a variable as part of another variables name.

For instance:

A="PRE"
eval ${A}_DAY=`date +%d`
echo ${A}_DAY
echo $PRE_DAY

output:
PRE_DAY
25

I would appreciate some help with this.
# 2  
Old 01-25-2008
Hi,

I'm not sure this answer suits to your request (if you meant
"how to display/store or use the resulting value", then this
should help...).

First, the 2 first lines sound correct for both defining a variable
and assigning a value to it:
me@host> A="PRE"
me@host> eval ${A}_DAY=`date +%d`

In order to solve this kind of trouble, I usually use this
kind of syntax:
me@host> DAY_VALUE=$(unalias echo; eval echo $"${A}_DAY")
me@host> echo ${DAY_VALUE}
me@host> 25

Hope it helps,
Christophe
# 3  
Old 01-25-2008
Quote:
Originally Posted by KittyWu
Hi,

I'm not sure this answer suits to your request (if you meant
"how to display/store or use the resulting value", then this
should help...).

First, the 2 first lines sound correct for both defining a variable
and assigning a value to it:
me@host> A="PRE"
me@host> eval ${A}_DAY=`date +%d`

In order to solve this kind of trouble, I usually use this
kind of syntax:
me@host> DAY_VALUE=$(unalias echo; eval echo $"${A}_DAY")
me@host> echo ${DAY_VALUE}
me@host> 25

Hope it helps,
Christophe
Ran into a couple of other ways...

echo $(eval echo \$${A}_DAY)
eval echo "\$${A}_DAY"

Thank You for your answer Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PASSING PART OF FILE CONTENT TO VARIABLE

All, I have a log file containing lots of data now i want to extract all text between block below(names) without the title or end pattern but only names, ++++START++++ SCOTT TIGER HENRY PAUL JARED OTIENO OMOLLO JA NIGERIA ++++END++++ the names i want to return and store in a variable in... (1 Reply)
Discussion started by: Scott2000
1 Replies

2. Shell Programming and Scripting

use some part of variable value to replace ../../ values in a file

hi, i have variable value as follows val="/dir1/dir2/dir3/dir4/dir5/dir6/file1" it is pointing to some file location. and i have another file as ../../dir4/file3 ../../dir4/dir5/file4 ../../dir7/file5 i want the output as /dir1/dir2/dir3/dir4/file3 (7 Replies)
Discussion started by: snreddy_gopu
7 Replies

3. Shell Programming and Scripting

Using the part of a line as a variable?

Hello Friends, I need a command (or script line) that allows me to use of a part of line (given by me) as a variable. Let us assume the name of the command is MYCMD. When I type MYCMD fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/ it must do the following job cd /homefolder/hhk/ADS/ ... (18 Replies)
Discussion started by: rpf
18 Replies

4. UNIX for Dummies Questions & Answers

Removing Part of a variable based on a pattern

Hi All, I am writing a script to work with files in a folder. The files are all in the following patterns (without quotes): "some filename - NxNN - the end.YYY" or "some filename - NNxNN - the end.YYY" Where N = a single number and YYY is the extension. Basically what I want... (5 Replies)
Discussion started by: sgtbobie
5 Replies

5. Shell Programming and Scripting

How read the part of the string into a variable?

Hi, I'm using bash and brand new to shell script. I would like to do the following. I have a string which is "UPDATE=1.0". I would like to read the value "1.0" alone in a variable. i.e the things afer "=" How do I do that? Thanks, (1 Reply)
Discussion started by: scriptfriend
1 Replies

6. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

7. Shell Programming and Scripting

Copy part of a variable

Hi, i was using a input file to get the last line of the file.But now i have stored the values from the file to a variable and want the last line from the variable . Slightly confused on how to extract that data from the variable. previous code, cat input.txt <TIME>00:15:48</TIME>... (2 Replies)
Discussion started by: Shellslave
2 Replies

8. Shell Programming and Scripting

direction symbol in a variable as part of the command

Hi, How can I get this to work? #!/bin/ksh if ; then direction=">>" else direction=">" fi cat some_file_name $direction temp.txt exit This shell script is not happy with using "$direction" opposed to ">" or ">>". Thanks. (5 Replies)
Discussion started by: peterloo
5 Replies

9. Shell Programming and Scripting

Repacing part of string with a variable

I have following strings in a file DUPTASMTRMMBAL,20070416200704160117232101172321,,,,,,,@@@Y DUPTASMTRMMCON,20070416200704160127189901271899,,,,,,,@@@Y DUPTASMTRMMHG,,20070416200704160112051001120510,,,,,,,@@@Y What i need to do is replace the date 20070416 with anoth date which is stored in... (4 Replies)
Discussion started by: divz
4 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question