LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash string to array (https://www.linuxquestions.org/questions/programming-9/bash-string-to-array-807478/)

patolfo 05-12-2010 05:46 PM

bash string to array
 
Hi there guys,

If i have this:
Code:

#!/bin/bash
clear
vary=$(echo "1 2 3 var1 var2" | awk '{print $4,$5}')
echo $vary
exit

i got:
Code:

myscripts>vary
var1 var2

How do i assign each of this values to a different bash variables, the only way i can think of is to convert it to an array? any other solution.

p.s.
Actually i am doing this:
Code:

#!/bin/bash
clear
vary1=$(echo "1 2 3 var1 var2" | awk '{print $4}')
vary2=$(echo "1 2 3 var1 var2" | awk '{print $5}')
exit

as you see i had to use twice the same command, any way of avoiding this?
Imagine i want to assign each of the data columns in the result to a new variable instead of the last two... :(

p.s.1. This data is fictitious, at least the echo part inside the "$()"

grail 05-12-2010 06:19 PM

Array sounds good to me, just throw another set of brackets outside $()

catkin 05-13-2010 01:30 AM

Cat skinning
Code:

# Here string
read x x x foo bar x <<< $data
echo "foo is '$foo' and bar is '$bar'"

# Here document
foo= bar=
read x x x foo bar x <<- EoF
    $data
EoF
echo "foo is '$foo' and bar is '$bar'"

# Array
foo= bar=
array=( $data )
foo=${array[3]}
bar=${array[4]}
echo "foo is '$foo' and bar is '$bar'"


grail 05-13-2010 01:52 AM

Quote:

Cat skinning
Well now that just sounds painful .... just jokes .. very nice, although for the array I would just refer directly to it rather than passing on to other variables.

catkin 05-13-2010 02:40 AM

Quote:

Originally Posted by grail (Post 3966659)
for the array I would just refer directly to it rather than passing on to other variables.

Me too (unless for legibility in a big program) but AIUI that would not meet the OP requirement.

patolfo 05-13-2010 12:50 PM

i do not get any of that code :(
 
And in plain english, for dummies you did?
care of explaining that piece of code please :(

Quote:

Originally Posted by catkin (Post 3966647)
Cat skinning
Code:

# Here string
read x x x foo bar x <<< $data
echo "foo is '$foo' and bar is '$bar'"

# Here document
foo= bar=
read x x x foo bar x <<- EoF
    $data
EoF
echo "foo is '$foo' and bar is '$bar'"

# Array
foo= bar=
array=( $data )
foo=${array[3]}
bar=${array[4]}
echo "foo is '$foo' and bar is '$bar'"



tuxdev 05-13-2010 01:21 PM

Just a few notes:

Code:

# Here string
read x x x foo bar x <<< $data
echo "foo is '$foo' and bar is '$bar'"

Instead of the dummy 'x', you can actually just use '_', like
Code:

read _ _ _ foo bar _ <<< "$data"
Code:

# Here document
foo= bar=
read x x x foo bar x <<- EoF
    $data
EoF
echo "foo is '$foo' and bar is '$bar'"

The <<- form of here-doc only strips tabs, not spaces. Makes it pretty much useless to me, since I hate tabs in my code.

patolfo:
http://mywiki.wooledge.org/BashGuide

catkin 05-13-2010 01:47 PM

Quote:

Originally Posted by patolfo (Post 3967247)
And in plain english, for dummies you did?
care of explaining that piece of code please :(

For the read command try help read at a bash prompt and find some examples here.

For the "here document" and "here string" see here.

catkin 05-13-2010 02:00 PM

Quote:

Originally Posted by tuxdev (Post 3967288)
Instead of the dummy 'x', you can actually just use '_'

Is there something special about "_"? I vaguely recall the ksh read command allowed "." as a placeholder and was wanting for something similar in bash but _ is a valid (if unusual) variable name. Or is it? This command prompt experiment did not work as expected:
Code:

c@CW8:~$ read _ rest
foo bar
c@CW8:~$ echo $_
rest
c@CW8:~$ echo $rest
bar
c@CW8:~$ read a b
foo bar
c@CW8:~$ echo $a
foo
c@CW8:~$ echo $b
bar


tuxdev 05-13-2010 02:06 PM

about $_:
Quote:

At shell startup, set to the absolute pathname used to invoke
the shell or shell script being executed as passed in the envi‐
ronment or argument list. Subsequently, expands to the last
argument to the previous command, after expansion. Also set to
the full pathname used to invoke each command executed and
placed in the environment exported to that command. When check‐
ing mail, this parameter holds the name of the mail file cur‐
rently being checked.
The _ in a read, though, is an explicit placeholder that tells bash to throw that value away, not a variable.

catkin 05-13-2010 03:27 PM

Thanks tuxdev :) I forgot to check bash special parameters. :redface:

patolfo 05-14-2010 04:55 PM

ok ok i will read more about bash redirection but...
 
I can not find what the triple <<<, stands for :(
read x x x foo bar x <<< $data

i know read takes data from console, keyboard, but the <<<, i might think << is necessary only

patolfo 05-14-2010 04:59 PM

Quote:

Originally Posted by grail (Post 3966356)
Array sounds good to me, just throw another set of brackets outside $()

And how so i did that and i got:
Code:

#!/bin/bash
clear
vary=[$(echo "1 2 3 var1 var2" | awk '{print $4,$5}')]
echo $vary
exit

Code:

[var1 var2]

patolfo 05-14-2010 05:08 PM

Code:

#!/bin/bash
# Here document
foo= bar=
read x x x foo bar x <<- EoF
    $data
EoF
echo "foo is '$foo' and bar is '$bar'"

exit

yep i am dense, but i got this output:
Code:

/myscripts>treintatreinta
foo is '' and bar is ''


grail 05-15-2010 12:39 AM

Quote:

vary=[$(echo "1 2 3 var1 var2" | awk '{print $4,$5}')]
The extra brackets should be round () not square []


All times are GMT -5. The time now is 04:09 AM.