create variable name based on another variable's value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting create variable name based on another variable's value
# 1  
Old 11-01-2007
create variable name based on another variable's value

Hello,
I am needing to create a variable and assign it a value based on the value of a previosly defined variable... I am using KSH..

Example:
VAR1=COMPUTER1
I need another variable like ${VAR1}_FLAG="Y", so it would actually be COMPUTER1_FLAG="Y".

I will be looping through many values in a while loop and I need to keep track if I have already processed a value already. So my data may look like this:

COMPUTER1
COMPUTER2
COMPUTER3
COMPUTER1
COMPUTER3

Initially the value of my second variable I am attempting to create would be N, if it ever gets turned to Y because it hit a specific condition then I need to know that later on after I have looped through the values. I will go through the values again in another while loop to see which ones are Y now so then I would need to do this:
If [ ${VAR1}_FLAG = "Y" ]
then
... do stuff
fi



Hope I have explained enough detail... Thanks for any help.
# 2  
Old 11-01-2007
Is there a reason you do not want to use arrays? Coding what you want will be a lot more inefficient. It's possible:
Code:
var=COMPUTER

for i in 1 2 3 4 5 6 7 8 9 
do
     print "$var"$i"=N"
done > t.sh
chmod +x t.sh
. t.sh

At this point COMPUTER1 is N.... COMPUTER9 is N
# 3  
Old 11-01-2007
You can do something like that :
Code:
VAR1=COMPUTER1

flag_name=${VAR1}_FLAG
eval ${flag_name}=N
eval flag_value=\$$flag_name

# With Bash

flag_name=${VAR1}_FLAG
eval ${flag_name}='Y'
flag_value=${!flag_name}

Execution output with trace option:
Code:
+ VAR1=COMPUTER1
+ flag_name=COMPUTER1_FLAG
+ eval COMPUTER1_FLAG=N
++ COMPUTER1_FLAG=N
+ eval 'flag_value=$COMPUTER1_FLAG'
++ flag_value=N
+ flag_name=COMPUTER1_FLAG
+ eval COMPUTER1_FLAG=Y
++ COMPUTER1_FLAG=Y
+ flag_value=Y

Jean-Pierre.

Last edited by aigles; 11-01-2007 at 03:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling a Variable based on a Variable

Hi all, I have a source config file with variables like so: eth1_ip=192.168.1.99 eth2_ip=192.168.1.123 eth3_ip=172.16.1.1 I am trying to run a script which loops based on the number of eth interfaces on a machine and therefore modifies the variable it calls in the environment based on the... (5 Replies)
Discussion started by: landossa
5 Replies

2. Shell Programming and Scripting

How to create variable from date

I have a requirement to send a trigger file (trigger_dayn.dat) to a remote server through the FTP trigger_dayn.dat - here the dayn is the day number. However the dayn is not sysdate. day 1 is from Monday 07:00.00 AM to Tuesday 06:59:59 AM and so on. So if the trigger file is generated say... (1 Reply)
Discussion started by: k_vikash
1 Replies

3. Shell Programming and Scripting

Variable to create a list

Hi all, I couldn't find an answer for this easy question, probably because the keywords I used in the search are too generic. I just want to make a list of numbers using the value of a variable, like this: NumFiles=$(ls | wc -l) for i in {1..$NumFiles}; do Say $NumFiles = 5. Bash... (3 Replies)
Discussion started by: Aquila
3 Replies

4. Shell Programming and Scripting

Reading content of a variable to create a new one?

Hello. I've written up a script, that populates a variable with a list of tapes returned from my library. For example: 701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3 So now, the variable "TAPELIST" contains those numbers, delimited by commas. I'd like to... (6 Replies)
Discussion started by: Stephan
6 Replies

5. Shell Programming and Scripting

Using awk to create files based on a variable name

Hey all, I am parsing a file which have records containing one of a number of files names: ".psd", ".cr2", ".crw" , ".cr", ".xi", ".jpg", ".xif" etc Somewhere on each line there is a value "Namex.psd" "Namex.crw" etc. The position of this name is highly variable I need to output all the ".psd"... (4 Replies)
Discussion started by: C0ppert0p
4 Replies

6. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

7. Programming

how to create variable for all thread in c++

hello I have this code #include <sys/types.h> #include <unistd.h> #include <iostream> #include <pthread.h> #include<cstring> using namespace std; int var1=0; void doSomething() { var1 = 5; cout<<"Do S :"<<var1<<endl; sleep(1); var1 =7; (4 Replies)
Discussion started by: vip_a1
4 Replies

8. UNIX for Dummies Questions & Answers

create a new environment variable??

Hi, I have say two programs File1: echo Enter A's Value read A export A File2: while do echo $A sleep 5 done (1 Reply)
Discussion started by: pbsrinivas
1 Replies

9. Shell Programming and Scripting

Create Variable for a PATH

Hello all, I need to create a variable that contains a path to a program. I'd like to reference that variable later in the script to execute the program. Does anyone have any ideas how I can accomplish this? Thanks, Mark :) (2 Replies)
Discussion started by: mmignot
2 Replies

10. Shell Programming and Scripting

Trying to create variable, value not being held

I tried creating a variable with the awk command. The correct value is being printed to the screen; however, the variable max is not being set. The command "echo $max" is null. Any help would be greatly appreciated. Thank you. cat test.txt: -rw-r--r-- 1 root root 0 2005-01-12 20:51... (2 Replies)
Discussion started by: cstovall
2 Replies
Login or Register to Ask a Question