Nested variable allocation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested variable allocation
# 1  
Old 04-10-2014
Nested variable allocation

I've made a number of errors with this and am trying to work a solution within the same framework.
Code:
/bin/ksh
for host_name in ahost1 ahost2 bhost1 bhost2
do
    for host_prefix in a b
    do
        if echo ${host_name} | grep -qi ${host_prefix}
        then
            if [ ${proc_check} -eq 0 ]
            then
                ${host_prefix}_active=${host_name}
            else
                ${host_prefix}_failover=${host_name}
            fi
        fi
    done
done
#
echo "a group active = ${a_active}"
echo "a group failover = ${a_failover}"
echo "b group active = ${b_active}"
echo "b group failover = ${b_failover}"

# 2  
Old 04-10-2014
I advise against dynamically-generated variable names in general, it's usually cleaner to use arrays, but they do have some uses for things like parsing configuration files.

The safest way to set an arbitrary variable name is read. This should work in most Bourne shells.

Code:
read -r ${host_prefix}_active <<EOF
${host_name}
EOF

Note that the last two lines must be at the beginning of the line, not indented.

In BASH you can make it much shorter:

Code:
read -r ${host_prefix}_active <<<"${host_name}"

KSH and BASH can also read the contents of a variable name like

Code:
VARNAME="${host_prefix}_active"
echo "contents of ${VARNAME} are ${!VARNAME}"

Other shells would need to use eval, which is a security nightmare.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add second variable in awk nested if condition?

Hi Gurus, I have a command to assign value based on input value. current condition is "if pattern matches "case", then assign "HOLD" else "SUCC"right now, I need to add one more condition (variable name is VAR). the condition is "if pattern1 matches "case", then assign "HOLD" else if... (2 Replies)
Discussion started by: ken6503
2 Replies

2. Shell Programming and Scripting

memory allocation to a variable

hello all.. i'm a beginner in shell scripting. I need to know what is really happening when we are creating a variable in shell scripting? how memory is allocated for that variable? (3 Replies)
Discussion started by: aarathy
3 Replies

3. Solaris

Block-based allocation and Extent-based allocation in Solaris

Hi guys! Could you tell me what's this figure about? (See the attached figure below.) This is a representation of block allocation filesystem and extent allocation filesystem in Solaris. Does this mean that in a block-based allocation, data are placed in individual blocks while in... (0 Replies)
Discussion started by: arah
0 Replies

4. UNIX for Advanced & Expert Users

How to parse nested variable

Hi, I want to parse nested variable in my script. for exp- c=1 G1='0318' G2='0023' G3='3092' G4='0014' while ;do g=G$c a=$g echo "Group=$g and value=$a" c=`expr $c + 1` done final output are as - --------------------------- Group=G1 and... (4 Replies)
Discussion started by: apskaushik
4 Replies

5. Programming

Memory allocation in C

Hi Experts I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables. First: int x; char a; printf("%u %u\n', &x, a); I got the addresses displayed... (2 Replies)
Discussion started by: unx_freak
2 Replies

6. Shell Programming and Scripting

nested double quota and white space inside variable

I have a question about nested double quotes. Any help is appreciated. Here are my commands on Mac OS. # string="Ethernet \"USB Ethernet\" \"Bluetooth DUN\" AirPort FireWire \"Bluetooth PAN\"" # echo $string Ethernet "USB Ethernet" "Bluetooth DUN" AirPort FireWire "Bluetooth PAN" #... (3 Replies)
Discussion started by: lindazhou
3 Replies

7. Programming

dynamic allocation vs static allocation in c

i wrote a tiny version of tail command using a large buffer statically allocated but, in a second time, i found another version in which i use a bidimensional array dynamically allocated. here is the first version /*my tiny tail, it prints the last 5 line of a file */ #include<stdio.h>... (4 Replies)
Discussion started by: lucasclaus
4 Replies

8. Shell Programming and Scripting

Enviornment Variable in B shell (I call it nested variable)

#!/bin/sh APP_ROOT_MODE1=/opt/app1.0 APP_ROOT_MODE2=/opt/app2.0 APP_ROOT=${APP_ROOT_${APP_MODE}} # enviornment variable APP_MODE will be exported in the terminal where # we run the applciation, its value is string - MODE1 or MODE2 # My intension is: # when export APP_MODE=MODE1... (4 Replies)
Discussion started by: princelinux
4 Replies

9. UNIX for Dummies Questions & Answers

HOw to get a variable allocation

HI Gurus, I had a requirement where i want to allocate a file name into a variable and get the file name in the subj of email. Suppose i have a file File002.pdx in the folder /home/pcs/system/files/File002.pdx Iam using a variable a = `ls /home/pcs/system/files/*.pdx` Iam using *... (2 Replies)
Discussion started by: pssandeep
2 Replies

10. Shell Programming and Scripting

Variable in While Loop Nested If

Hi all, I'm having problems with the setting a variable in a nested if statement. It doesn't seem to change even if it mets the 'if' condition. My script essentially looks for a user name from the output from a kerberos command. When I find the user name, I tried to change a variable and exit... (6 Replies)
Discussion started by: geass
6 Replies
Login or Register to Ask a Question