Storing Multiple Values in a Variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Storing Multiple Values in a Variable
# 1  
Old 03-02-2009
Storing Multiple Values in a Variable

Hi,

My code is as below:

integer i=7
while ((i <= 5 ));
do
# test_out is a variable which contains data separated by "^".

a= `echo $test_out | cut -d"^" -f$i`

echo "$a"

(( i = i + 1));
done



From the above code, i kept $i after f so that i can capture all the data which is separated by "^". Here since "i" is incrementing 5 times, i want to store all the 5 values in variables such as a1,a2,a3,a4,a5 .

How can i do this. If i keep "i" after a it is not working. Ideally instead of keeping 5 commands for a, i want to do it in single shot by keeping ai. But it is not working.

If i do an echo "$ai", it must display all the 5 values since its looping 5 times.

Correct me if i mentioned some thing wrong. I am new to UNIX so it would be great if some body can help me out.

Thanks
Sandeep
# 2  
Old 03-03-2009
Assuming you are using the Korn shell, the following example should show you how to do what you want
Code:
integer i=1

while ((i <= 5 ))
do
   eval 'a'$i'=$i'
   (( i++ ))
done

echo "$a1 $a2 $a3 $a4 $a5"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. Shell Programming and Scripting

How to perform multiple operations on a number before storing to a variable?

(I am using bash) I have a command that will find the line number in a file by searching for a string where it exists. linenumber=$(grep -n "string" $FILENAME | cut -d : -fi) This returns the line number and removes the string. Now that I have the line number I want to subtract 4 from it and... (5 Replies)
Discussion started by: prodigious8
5 Replies

3. Shell Programming and Scripting

Interpreting multiple values from a variable

Hi, I am writing a shell script which will check the status of a resource in a cluster and then display nicely to a user running the script at command line. Basically the script runs a status command and then pulls certain keywords from the return and then should display a concise status. ... (5 Replies)
Discussion started by: chris01010
5 Replies

4. Shell Programming and Scripting

Variable with multiple values

Hello I need to alter a script to check for a flag file but there are now more than one type of flag file in my directory. Basically if any of these flg files exist then the MASK value should be set? Flag files to be included in assignment to variable e2e_scheduled*.flg COLL_STOP*.flg... (1 Reply)
Discussion started by: andymay
1 Replies

5. Shell Programming and Scripting

Removing multiple values from a variable

Hi I have a requirement like # e=`ls | grep -e"test" | awk -F " " '{print $1}'` (0) root @ ptxfv3: / # echo $e test test.xml From this i need to grep the word "test" alone i.e., it is occuring twice I need only one among them Please help (6 Replies)
Discussion started by: Priya Amaresh
6 Replies

6. Shell Programming and Scripting

Storing multiple file paths in a variable

I am working on a script for Mac OS X that, among many other things, gets a list of all the installed Applications. I am pulling the list from the system_profiler command and formatting it using grep and awk. The problem is that I want to be able to use each result individually later in the script.... (3 Replies)
Discussion started by: cranfordio
3 Replies

7. Shell Programming and Scripting

storing multiple values in a array variable

Am using a find command in my script .The output may be one or more. I need to store those values in a array and need to access those. Am unable to find the solution . Any help on this will be helpful. if < code> else a=<find command output which gives the file name either 1 or more> if 1... (1 Reply)
Discussion started by: rogerben
1 Replies

8. Shell Programming and Scripting

help with assigning multiple values to a variable

I have a situation where my variable needs to pick up any of the 4 values from the environment it is in for e.g i am on server named a server=a (script running on this server) ftp servers= b c d e ----- the parameter passed should be any of these values in these 4 values, if not throw an... (4 Replies)
Discussion started by: dsravan
4 Replies

9. UNIX for Dummies Questions & Answers

Storing values in shell variable

Hi, I am writing a shell script where, x=y y=z When I want to print z, I can do $y How do I use only "x" without any direct reference to "y" to print z? Thanks, -G (3 Replies)
Discussion started by: gaurab
3 Replies

10. Shell Programming and Scripting

Storing values in variable

Hi All, Here is the description of the problem. I am scripting for database access using k-shell on solaris box dbaccess <databasename> - << EOF 2>/dev/null | awk 'BEGIN {FS=" "}\ {printf "%s", $1}' | grep -v "^$" | \ read cnt1 OUTPUT TO PIPE cat WITHOUT HEADINGS select count(*) from... (1 Reply)
Discussion started by: matrixmadhan
1 Replies
Login or Register to Ask a Question