1. 'echo "some text" |read foo' results in empty $foo
Hi!
In a script that I'm working on, I do something like this:
#!/bin/sh
echo "this is some text" |read foo bar baz
echo "the foo variable is: ${foo}" # should be 'this'
echo "the bar variable is: ${bar}" # should be 'is'
echo "the baz variable is: ${baz}" # should be 'some text'
However, the output leaves no doubt that the three variables are unset.
TFM says:
read name ...
One line is read from the standard input and, using the
internal field separator, IFS (normally space or tab),
to delimit word boundaries, the first word is assigned
to the first name, the second word to the second name,
and so forth, with leftover words assigned to the last
name. Lines can be continued using \newline. Charac-
ters other than newline can be quoted by preceding them
with a backslash. These backslashes are removed before
words are assigned to names, and no interpretation is
done on the character that follows the backslash. The
return code is 0, unless an EOF is encountered.
So I'm really not sure why it doesn't work. This is /bin/sh under Solaris
2.6, but of course I want the solution to be portable.
I could do something like
foo=`echo "this is some text" | awk '{ print $1 }'`
bar=`echo "this is some text" | awk '{ print $2 }'`
and so on... but that's both ugly and wasteful.
Replies sent directly to me will be summarized for the newsgroup.
Happy Hunting, -Chris
2. RedHat 5.1 Install LI.. Problem.
3. help: "emacs:can't load library 'libXt.so.6'" . . .
4. Unmounting Drives During Power Down.
5. "can't load library 'libXmu.so.6'"
6. Help On FTP VIA HTTP -- Browser does not list ftp directory?
7. Help: "tcsh: can't load library 'libc.so.5'" ??
8. Adding >4GB disk to rootvg
9. "can't load library 'libXpm.so.4'"
10. Why doesn't echo "text" 'command' "more text" work?
11. "can't find library 'libX11.so.6'"
12. "Can't find shared library 'libc.so.3.0'"