LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-12-2010, 06:46 PM   #1
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Rep: Reputation: 15
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 "$()"

Last edited by patolfo; 05-12-2010 at 06:48 PM.
 
Old 05-12-2010, 07:19 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Array sounds good to me, just throw another set of brackets outside $()
 
Old 05-13-2010, 02:30 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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'"
 
Old 05-13-2010, 02:52 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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.
 
Old 05-13-2010, 03:40 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by grail View Post
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.
 
Old 05-13-2010, 01:50 PM   #6
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Original Poster
Rep: Reputation: 15
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 View Post
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'"
 
Old 05-13-2010, 02:21 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
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
 
Old 05-13-2010, 02:47 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by patolfo View Post
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.

Last edited by catkin; 05-13-2010 at 03:01 PM. Reason: fur matting
 
Old 05-13-2010, 03:00 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by tuxdev View Post
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
 
Old 05-13-2010, 03:06 PM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
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.
 
1 members found this post helpful.
Old 05-13-2010, 04:27 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Thanks tuxdev I forgot to check bash special parameters.
 
Old 05-14-2010, 05:55 PM   #12
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Original Poster
Rep: Reputation: 15
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
 
Old 05-14-2010, 05:59 PM   #13
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
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]
 
Old 05-14-2010, 06:08 PM   #14
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Original Poster
Rep: Reputation: 15
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 ''
 
Old 05-15-2010, 01:39 AM   #15
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

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

Last edited by grail; 05-15-2010 at 06:09 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 11:07 AM
Problem with getting string value from array _kostek_ Programming 5 10-31-2009 12:11 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-03-2009 12:18 AM
To read a string into an array som_kurian Programming 7 12-06-2007 05:39 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 02:39 PM


All times are GMT -5. The time now is 03:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration