tag:blogger.com,1999:blog-462006525194985726.post2439541955194340077..comments2024-03-24T12:41:56.165-04:00Comments on Sandro Tosi: Read multiple variables from stdin in a bash scriptSandro Tosihttp://www.blogger.com/profile/17077191422205823991noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-462006525194985726.post-53185376175863360062011-06-16T09:13:59.087-04:002011-06-16T09:13:59.087-04:00read a b c <<<"A B C" # is more...read a b c <<<"A B C" # is more portable, but also only to modern shells.<br /><br />echo "A B C" |& read -p a b c # only works on Korn shells, not GNU bash…<br /><br />Something like this ought to work in POSIX shells:<br /><br />read a b c <<EOF<br />A B C<br />EOF<br /><br />Feel free to say hi in either #!/bin/mksh (yes that’s a channel name) or #ksh in Freenode IRC.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-74075274851086435352011-06-16T07:37:14.808-04:002011-06-16T07:37:14.808-04:00very simple and posix friendly solution:
echo &qu...very simple and posix friendly solution:<br /><br />echo "A BB CCC" | (<br /> read a b c<br /> echo $a $b $c<br />)<br /><br />this makes sure read/echo are in the same subshell. alternatively, you could/should use a function:<br /><br />foo() {<br /> read a b c<br /> echo $a $b $c<br />}<br /><br />echo A BB CCC | foo<br /><br />i'd advise against using set, at least unless you're absolutely sure about your input, since it might not do exactly what you want it to do in some cases.seaniushttps://www.blogger.com/profile/18193771013190375826noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-86276346900263391412011-06-15T18:11:14.951-04:002011-06-15T18:11:14.951-04:00How about the old trick with set?:
set -- `echo &q...How about the old trick with set?:<br />set -- `echo "A BB CCC"`<br />echo $1 $2 $3boranhttps://www.blogger.com/profile/11799893287020413357noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-15290165934500056522011-06-15T08:22:32.030-04:002011-06-15T08:22:32.030-04:00josch,
Maybe a quick and dirty perl script with ...josch, <br /><br />Maybe a quick and dirty perl script with eval' evil :-) magick:<br /><br />eval $(echo "a b c" | perl -ne '@vars=qw(a b c); s/((?{$a=$vars[$b]; $b=$b+1;})\w+) ?/$a=\1\n/g; print')<br /><br />Is this posix ?? (I don't know) but it can replace read builtin function (in its basic call, of course)Javihttps://www.blogger.com/profile/05221050894686905099noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-81346836727470023332011-06-15T07:53:31.531-04:002011-06-15T07:53:31.531-04:00now someone has just to find a way how to do this ...now someone has just to find a way how to do this with posix compatible with sh - because there is no process substitution or here documents in posixAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-89001590210881084692011-06-15T06:57:04.471-04:002011-06-15T06:57:04.471-04:00read a b c <<<"A B C"
gebi: Fr...read a b c <<<"A B C"<br /><br />gebi: From bash 2.0.5bJavihttps://www.blogger.com/profile/05221050894686905099noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-78309906274930811462011-06-15T06:03:44.715-04:002011-06-15T06:03:44.715-04:00% echo "A BB CCC" | read a b c; echo &qu...% echo "A BB CCC" | read a b c; echo "$a-$b-$c"<br />A-BB-CCC<br /><br />zshgebihttps://www.blogger.com/profile/02726867824497339744noreply@blogger.com