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

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

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

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

Any pointers would be good. I've tried searching but no joy :frowning:

#!/bin/ksh

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

Yes, use arrays (the syntax varies between shell implementations).

With bash, zsh and ksh93:

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

To access the array elements:

${array_name[n]}

[1] If you need a different word separator, you should modify IFS.

Thanks the replies.

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

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.

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:

$ 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):

$ /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 ----------

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

print "disk1=" $2 ...

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 :slight_smile:

Cheers