Read variables names from array and assign the values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read variables names from array and assign the values
# 1  
Old 09-29-2011
Read variables names from array and assign the values

Hi,

I have requirement to assign values to variables which are created dynamically.

Below is the code which i am using to achieve above requirement.

Code:
#!/bin/ksh

oIFS="$IFS"; IFS=',' 

STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3"
set -A WF_FAIL_PARENT_IF_FAILS $STR_FAIL_PARENT_IF_FAILS
wfnodeval4taskin="<TASKINSTANCE DESCRIPTION='' FAIL_PARENT_IF_INSTANCE_DID_NOT_RUN='YES' FAIL_PARENT_IF_INSTANCE_FAILS='YES' ISENABLED='NO' NAME='EventWait' REUSABLE='NO' TASKNAME='EventWait' TASKTYPE='Event Wait' TREAT_INPUTLINK_AS_AND='YES'/>"
echo "Length:${#WF_FAIL_PARENT_IF_FAILS[@]}"
for WF_TREAT_LINKS_VAL in ${WF_FAIL_PARENT_IF_FAILS[@]}; 
do
if [ -z $WF_TREAT_LINKS_VAL ]; then
    $WF_TREAT_LINKS_VAL="$(echo $wfnodeval4taskin | grep -o " TREAT_INPUTLINK_AS_AND='[^']*'" | sed 's/^ *//;s/ *$//')"
    break
fi
done
IFS="$oIFS"

wfnodeval4taskin string value is dynamic which is coming from file. Now i want to read all values from Array and assign values to that in case if that variables has no value.

I want to check first $WF_TREAT_LINKS_VAL is null or not if not null loop and until i get variable which has no value and assign derived value.

Finally i want to assign value to WF_F_P_IF_FAILS1=xxxxx

Thanks for your help.
Mallik.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 09-29-2011 at 09:17 AM..
# 2  
Old 09-29-2011
Use eval to do this:

Code:
#!/bin/ksh
 
oIFS="$IFS"; IFS=','
 
STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3"
set -A WF_FAIL_PARENT_IF_FAILS $STR_FAIL_PARENT_IF_FAILS
wfnodeval4taskin="<TASKINSTANCE DESCRIPTION='' FAIL_PARENT_IF_INSTANCE_DID_NOT_RUN='YES' FAIL_PARENT_IF_INSTANCE_FAILS='YES' ISENABLED='NO' NAME='EventWait' REUSABLE='NO' TASKNAME='EventWait' TASKTYPE='Event Wait' TREAT_INPUTLINK_AS_AND='YES'/>"
echo "Length:${#WF_FAIL_PARENT_IF_FAILS[@]}"
for WF_TREAT_LINKS_VAL in ${WF_FAIL_PARENT_IF_FAILS[@]};
do
   CUR_VAL=$(eval echo \$$WF_TREAT_LINKS_VAL)
   if [ -z "$CUR_VAL" ]; then
      eval $WF_TREAT_LINKS_VAL="$(echo $wfnodeval4taskin | grep -o " TREAT_INPUTLINK_AS_AND='[^']*'" | sed 's/^ *//;s/ *$//')"
      break
   fi
done
IFS="$oIFS"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How To Read a File and Assign the line values to an Array?

i have this basic code that i wrote to read a file and place it's values to an array. the source/input file will have multiple strings on it that is separated by a whitespace. sample_list.txt file contents: ACCT1 TABLE1 ACCT2 TABLE2 ACCT3 TABLE3 script file: sample_list.sh ... (3 Replies)
Discussion started by: wtolentino
3 Replies

2. Shell Programming and Scripting

Assign Unknown Values to Variables

i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea. but, i want to be able to assign each of the value returned by this program to a variable. in the latest instance, the program gave the following 6 values: 4... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

A better way to assign values to variables - shell

so i've been used to doing it this way: SVAL=$(echo "7 3 2 38 3" | awk '{print $2}') 4VAL=$(echo "4:21:N:3" | awk -F":" '{print $4}') I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont... (9 Replies)
Discussion started by: SkySmart
9 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space Input file server1 server2 server3 server4 server5 server6 When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values while read a b do echo "$a" echo... (5 Replies)
Discussion started by: chidori
5 Replies

6. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

7. Shell Programming and Scripting

how to assign file names to array variable?

I wish to assign file names with particular extention to array variables. For example if there are 5 files with .dat extention in /home/sam then i have to assign these 5 files to an array. plz help me how to accomplish this. Thanks in advance. (4 Replies)
Discussion started by: siteregsam
4 Replies

8. Shell Programming and Scripting

Assign values to variables of a file

Hi, I have a file like the following... CUST= DIR= NULIST= name=philps_123 How can i add values to each of these unassigned variables using a shell script? say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
Discussion started by: Tuxidow
11 Replies

9. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

10. UNIX for Dummies Questions & Answers

to assign cut values to an array

i need to seperate values seperated by delimiters and assign it to an array.. can u plz help me on that. Variables = "asd,rgbh,(,rty,got,),sroe,9034," i need to assign the variables into arrays.. like.. var=asd var=rgbh.. and so on how do i do this. i need to reuse the values stored in... (6 Replies)
Discussion started by: Syms
6 Replies
Login or Register to Ask a Question