Calling a variable from a variable.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Calling a variable from a variable.
# 1  
Old 03-18-2013
Calling a variable from a variable.

Hi everyone,

Is it possible to set a variable that calls another variable?

I.E.
Code:
SCRIPT=MY_SCRIPT.ksh ${VAR5}

${VAR5} is set earlier in the script, and I want to be able to call this when setting the ${SCRIPT} variable.

I hope this makes sense.

Thanks for your help.
# 2  
Old 03-18-2013
Wrap it in double quotes and use eval
Code:
SCRIPT="./MY_SCRIPT.ksh ${VAR5}"

eval "$SCRIPT"

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-20-2013
Quote:
Originally Posted by Yoda
Wrap it in double quotes and use eval
Code:
SCRIPT="./MY_SCRIPT.ksh ${VAR5}"

eval "$SCRIPT"

Not exactly. When we look at:
Code:
SCRIPT="./MY_SCRIPT.ksh ${VAR5}"
eval "$SCRIPT"

What is being suggested here is probably not what is wanted.

If the intent is to expand $VAR5 at the time SCRIPT is defined, then the eval is not needed when script is invoked to run MY_SCRIPT.
Code:
SCRIPT="./MY_SCRIPT.ksh ${VAR5}"
eval "$SCRIPT"

But, if the intent is to have the shell expand $VAR5 at the time that $SCRIPT is invoked, then single quotes (rather than double quotes) need to be used when SCRIPT is defined.

As an example, look at the following sequence of commands:
Code:
VAR5=initial
SCRIPT="echo $VAR5"     # Note the double quotes
VAR5=middle
$SCRIPT                 # produces: initial
"$SCRIPT"               # produces: echo initial: not found
eval "$SCRIPT"          # also produces: initial
SCRIPT='echo $VAR5'     # Note single quotes: VAR5 is currently set to middle
VAR5=final
$SCRIPT                 # produces: $VAR5
"$SCRIPT"               # produces echo $VAR5: not found
eval $SCRIPT            # produces: final

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 03-20-2013
eval is dangerous. Why not save just the script name, and feed the variable into it later?
This User Gave Thanks to Corona688 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling specific characters from a find variable

I'm trying to do something like this: find . -name blablabla -exec ln -s ./"{:53:14} blablabla" \; The idea is find blablabla and create a symbolic link to it using part of it's path and then it's name, "blablabla." I just don't know if I can call characters out of a find variable. ... (16 Replies)
Discussion started by: scribling
16 Replies

2. Shell Programming and Scripting

Calling a variable of variable from a file

Hi All, I have file which have looks like below abc=${def} def=${efg} efg= "this is the actual value" based on "abc" value I have to call "efg" value , Am using below lines but it is not working #!/bin/bash source file.txt echo $abc Please wrap all code, files, input &... (5 Replies)
Discussion started by: Prashanth.K
5 Replies

3. 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

4. Shell Programming and Scripting

Csh variable calling problem

First post on here. So I use csh shells for my research (physics... not a CS person). I am trying to rerun the same scripts, but there are ~10 files that have similar variables that I have to change for each different configuration, so I would like one central file for the variables I change that... (3 Replies)
Discussion started by: sabrepride
3 Replies

5. Shell Programming and Scripting

Calling a variable in another variable

echo "$previous_tmp$i" I have a 5 variables like previous1 previous2 previous3 previous4 previous5 I want to use a for loop to call them one by one. How can I ?:confused: (2 Replies)
Discussion started by: Junaid Subhani
2 Replies

6. Shell Programming and Scripting

PERL script -- calling 'sed' by passing 'variable value'.

Hi Friends, I'm calling 'sed' command inside one perl script, which is to list directory names which are having some date value as their names (in the form YYYYMMDD) with in the range (start and end date). #!/usr/bin/perl -w use strict; use warnings; my $DATA = "/export/home/ganapa"; my... (5 Replies)
Discussion started by: ganapati
5 Replies

7. 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

8. UNIX for Dummies Questions & Answers

Calling a function through a variable

Hey folks, I'm pretty new to unix programming. I was trying to get something to work but it's not doing what I expected. #!/bin/ksh . ./functions.sh STRING=function_1 FUNCTION="$STRING" RETURN=eval $FUNCTION echo "value of $FUNCTION function is: $RETURN" All i'm... (5 Replies)
Discussion started by: Irrational
5 Replies

9. Shell Programming and Scripting

calling a variable to echo to a log

Hi everyone, I am trying to create a simple batch file to make SQL backups. this part of it works fine. Currently the script can mysql dump the databases, compress them, delete the .sql, compress the individual tar.gz into one larger one, delete the smaller files, encrypt the final tar.gz and... (1 Reply)
Discussion started by: luma
1 Replies

10. Shell Programming and Scripting

calling a aliased variable

Issue: i have variable A which is an alias for variable B which is equal to "THIS IS A TEST" when every i echo variable A i only get the alias name for variable B, NOT the contents of variable B. HOSTNAME# echo $TESTIT + echo THIS IS A TEST THIS IS A TEST HOSTNAME# ls -l total... (10 Replies)
Discussion started by: Optimus_P
10 Replies
Login or Register to Ask a Question