store multiple variables in one go


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting store multiple variables in one go
# 1  
Old 05-14-2010
store multiple variables in one go

Guys anyone know how i can store fields into multiple variables in one go?

I'm wanting to grab the disk id's from format into disk1 and disk2

Below is what i want to work but i know it doesnt :-

Code:
: | format | awk '/^(\ +)[0|1]/ {print $2}' | read disk1 disk2

The below does work...but i don't want to do anything in the while loop. I basically want to run a command and store 2 variables.

Code:
# : | format | awk '/^(\ +)[0|1]/ {print $2}'|while read disk1 disk2;do echo "Hello $disk1 $disk2";done
Hello c0t0d0
Hello c0t8d0

I KNOW i could run the same command twice (as below), but thats just bad coding IMO.

Code:
disk1=$(: | format | awk '/^(\ +)0/ {print $2}')
disk2=$(: | format | awk '/^(\ +)1/ {print $2}')

Any pointers would be good. I've tried searching but no joy Smilie
# 2  
Old 05-14-2010
Code:
#!/bin/ksh

eval $(: | format | awk '/^(\ +)0/ {print disk1=$2}; /^(\ +)1/ {print disk2=$2}')

# 3  
Old 05-14-2010
Yes, use arrays (the syntax varies between shell implementations).

With bash, zsh and ksh93:

Code:
array_name=($(command_that_returns_multiple_space_separated[1]_values))

To access the array elements:

Code:
${array_name[n]}

[1] If you need a different word separator, you should modify IFS.
# 4  
Old 05-14-2010
Thanks the replies.

The eval basically doesnt work as i'm not trying to run the return :-

Code:
host# cat l
#!/bin/ksh

eval $(: | format | awk '/^(\ +)0/ {print disk1=$2}; /^(\ +)1/ {print disk2=$2}')

echo "disk1 = $disk1 | disk2 = $disk2"
host# ./l
./l[3]: c0t0d0:  not found
disk1 =  | disk2 =

The array seems a good idea, and i've used arrays briefly in the past. I'm having trouble with IFS though. This is running on Solaris8 using ksh.

Code:
host# cat m
#!/bin/ksh

oldIFS=$IFS

array=$(: | format | awk '/^(\ +)[0|1]/ {printf "%s ", $2}')

disk1=${array[0]}
disk2=${array[1]}

echo "disk1 = $disk1 | disk2 = $disk2"
host# ./m
disk1 = c0t0d0 c0t8d0  | disk2 =
host#

# 5  
Old 05-14-2010
Quote:
Originally Posted by lavascript
[...][The array seems a good idea, and i've used arrays briefly in the past. I'm having trouble with IFS though. This is running on Solaris8 using ksh.

Code:
host# cat m
#!/bin/ksh

oldIFS=$IFS

array=$(: | format | awk '/^(\ +)[0|1]/ {printf "%s ", $2}')

disk1=${array[0]}
disk2=${array[1]}

echo "disk1 = $disk1 | disk2 = $disk2"
host# ./m
disk1 = c0t0d0 c0t8d0  | disk2 =
host#

You're using ksh88 and ksh88 doesn't support that syntax.

You should either use its supported syntax:

Code:
$ set -A arr $(print your_command will produce one two)
$ print ${arr[0]}
your_command
$ print ${arr[3]}
one
$ print ${arr[@]}
your_command will produce one two

Or use ksh93 (if available under /usr/dt/bin/dtksh):

Code:
$ /usr/dt/bin/dtksh
$ arr=($(print your_command will produce one two))
$ print ${arr[0]}
your_command

You don't need to modify IFS in this case.

---------- Post updated at 03:40 PM ---------- Previous update was at 03:37 PM ----------

Quote:
Originally Posted by lavascript
Thanks the replies.

The eval basically doesnt work as i'm not trying to run the return :-

Code:
host# cat l
#!/bin/ksh

eval $(: | format | awk '/^(\ +)0/ {print disk1=$2}; /^(\ +)1/ {print disk2=$2}')

echo "disk1 = $disk1 | disk2 = $disk2"
host# ./l
./l[3]: c0t0d0:  not found
disk1 =  | disk2 =

You should quote disk1= and disk2= to make it work:

Code:
print "disk1=" $2 ...

# 6  
Old 05-14-2010
Thanks dudes.

set -A array does the trick.

I couldn't get eval working surprisingly, as i've used that before successfully. I'll just go with what works as time is always an issue Smilie

Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store output in variables instead of files

Hi All, I have written a (bash) function which generates multiple files say file1 file2 file3 now I want to reduce the generation of these three files i.e. store the output of three files in variables, and generate the same results, in-order to avoid multiple creation of files how is that... (7 Replies)
Discussion started by: sam@sam
7 Replies

2. Linux

How to store count of multiple queries in variables in a shell script?

how to store the count of queries in variables inside a filein shell script my output : filename ------- variable1=result from 1st query variable2=result from 2nd query . . . . (3 Replies)
Discussion started by: sanvel
3 Replies

3. Shell Programming and Scripting

How to store results of multiple sql queries in shell variables in ksh?

Hi, I have a script where I make a sqlplus connection. In the script I have multiple sql queries within that sqlplus connection. I want the result of the queries to be stored in shell variables declared earlier. I dont want to use procedures. Is there anyway else. Thanks in advance.. Cheers (6 Replies)
Discussion started by: gonchusirsa
6 Replies

4. Shell Programming and Scripting

Can -v option in awk be used to store an array of variables?

I want to pass an array of variables to be inserted by awk in the 2nd column of a file. Empl No. Employee Age 1000000 22 1100000 24 1200000 26 Now, I want to pass an array having three different ages which need to replace the... (7 Replies)
Discussion started by: Nishi_Licious
7 Replies

5. Shell Programming and Scripting

saving all input name and store them as variables

Hi I want to write a script such that when executed, it will store all input as different variable, for eg ./store.sh name1 name2 name3 name4 will result in $1=name1 $2=name2 $3=name3 etc How do I do that? Thanks. (1 Reply)
Discussion started by: piynik
1 Replies

6. Shell Programming and Scripting

Split lines in a file and store them in variables

Hi, I have a file called files.txt which contains data like http://abc.xyz.com/ghi/klm/nop/qrs/tuv/wxyz/ There are multiple lines like the above in this file. In .sh script, I would like to read this file, split each line, and store the variable of "qrs" and "wxyz" in separate variables. ... (3 Replies)
Discussion started by: archana.n
3 Replies

7. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

8. Shell Programming and Scripting

[Doubt] How can I store numbers less than 1? Shell variables

Hi everyone, I am having some problems with my scripts so I hope you could help me. I am trying to store the result of a division in a variable in tcshell but I have the problem that if: For example, dividing 2/100 the result is 0.02 but if I store that I have "0". How can I have 0.02... (8 Replies)
Discussion started by: Bloody
8 Replies

9. UNIX for Dummies Questions & Answers

trying to store variables in an array

im looping through an array setting three variables each time (one of the variables gives me the position in the array and is incremented each loop) im trying to then set the variables to that position in the array without much luck. any ideas? anArray=`$VAR1+$VAR2+"("$pos")"` (1 Reply)
Discussion started by: magnia
1 Replies

10. Shell Programming and Scripting

Extract numbers from a string and store in variables

Hi All, Is it possible in Unix shell script to extract numbers from a string containing ".", such as; 5.2.314 And store in variables so; var1 = 5 var2 = 2 var3 = 314 Thanks in advance for any help anyone can provide dave (6 Replies)
Discussion started by: davewg
6 Replies
Login or Register to Ask a Question