Runtime variable extraction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Runtime variable extraction
# 1  
Old 12-19-2014
Runtime variable extraction

Hi There,

var1=value1
var2=value2
var3=value3

for (( i=1; i<4; i++ ))
do
temp=var$i
echo "$temp"
done

OUTPUT
var1
var2
var3


I want the output to be
value1
value2
value3


How can I achieve this?

Regrads
Jagpreet
# 2  
Old 12-19-2014
There's a specific construct called an array which associates values with an index, assuming this is a bash script, take a look at the chapter on arrays in the bash beginners guide otherwise do a search for arrays in the documentation of this shell
# 3  
Old 12-19-2014
Thanks for the reply.

eval did the trick.

Thanks anyway
# 4  
Old 12-19-2014
Please use code tags as required by forum rules!

With a recent bash, you can try
Code:
for i in var{1..4}; do temp=${!i}; echo "$temp"; done
value1
value2
value3

# 5  
Old 12-19-2014
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!

With a recent bash, you can try
Code:
for i in var{1..4}; do temp=${!i}; echo "$temp"; done
value1
value2
value3

You can, the question is should you when there's a perfectly good array construct available?
# 6  
Old 12-19-2014
Quote:
Originally Posted by Skrynesaver
You can, the question is should you when there's a perfectly good array construct available?
Depends on the problem being solved, which we know nothing about. Arrays are used to solve far too many "problems" in shell, sometimes used to excess.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Runtime input

Hi all.. I have a command for example /abc/def/ghi.jkl Filename filename1. If I run this command, it will ask for y/N which I have to type manually. Now Im trying to automate it using shell script and input the y option. Please help me on doing this. Thanks in advance. ... (7 Replies)
Discussion started by: Sathya83aa
7 Replies

2. Shell Programming and Scripting

Evaluate Variable At Runtime

Hi, I am trying to set a variable that has time the format desired. And my intention is to echo variable (instead of actual date command) everytime I like to echo date. Please take a look at below code. $NOW='' echo $NOW After 5 minutes $echo $NOW Issue here is , I am not... (2 Replies)
Discussion started by: vinay4889
2 Replies

3. Shell Programming and Scripting

Data extraction in perl variable

HI, i have variable in perl like below $tmp="/home/sai/automation/work/TFP_GN.txt" it can conatain any path its filled from config file. now i want extarct the path upto this /home/sai/automation/work/ and put it in another variable say... (4 Replies)
Discussion started by: raghavendra.nsn
4 Replies

4. Shell Programming and Scripting

generating the variable list for WHILE READ statement at runtime

Hi, I am reading the contents of a file in variables as - cat ${var_file_name} | while read COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11 The problem is ... my file can have any number of columns - 5, 10, 11 .... So i want a dynamic variable list as - cat ${var_file_name} |... (8 Replies)
Discussion started by: gopalss
8 Replies

5. Solaris

Re:How to get the path during runtime?

Thank u vgesh99 It works well.. (1 Reply)
Discussion started by: Nandagopal
1 Replies

6. Solaris

How to get the path during runtime?

Hi guys, I have commands like /solaris/opt/VRTS/bin/vxdisk /opt/VRTS/bin/vxdisk From these two commands, i need to get the directory path value before /VRTS/bin/vxdisk at runtime. e.g /solaris/opt/VRTS/bin/vxdisk as /solaris/opt /opt/VRTS/bin/vxdisk as /opt I have tried with cut... (1 Reply)
Discussion started by: Nandagopal
1 Replies

7. UNIX for Dummies Questions & Answers

Runtime Error...

My system did stay appears the error Run Time Library Error. What it´s? When the error appear, i´ve to reboot my system and lost all I did. Is there the UNIX System problem? Please. I need help!!! (4 Replies)
Discussion started by: marpin
4 Replies

8. Solaris

Runtime error...

My interprise use a UNIX mainform for a instrumentation process control. The control use the FOXBORO INVENSYS system and they don´t gonna solve the problem the run time error. The run time error happen without logic explication. When everything it´s run perfectely and happenly appears the run time... (1 Reply)
Discussion started by: marpin
1 Replies

9. Shell Programming and Scripting

Getting Function Name At Runtime

Hi, Suppose I have a User define function get_abc in which I am using $0 to get the name of function. But when I call that function in any script, $0 will give the script name, not the function name. For example: Function: get_abc ------------------- get_abc( ){ echo $0 } Script:... (3 Replies)
Discussion started by: yeheyaansari
3 Replies
Login or Register to Ask a Question