LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 07-28-2004, 02:59 PM   #1
kc8tbe
Member
 
Registered: Feb 2003
Location: Cleveland, Ohio (USA)
Distribution: Gentoo, Kubuntu 6.06
Posts: 179

Rep: Reputation: 30
the trouble with |read


I sort of expected this to work:
Code:
unset STRING #make sure we start with a clean slate
echo "wassup" | read STRING # note that this line produces no output
echo $STRING # though it ought to, this line produces no output
I've also tried some variations of read, including those that ignore the newline character. For some reason, "wassup" isn't assigned to $STRING. Since line 2 produces no output, it's safe to assume that "wassup" is being piped to read. And since running line 2 with "read -t 3" returns instantaneously (instead of waiting three seconds), it's safe to assume that read is seeing some data. Anyone know what I'm doing wrong?
 
Old 07-28-2004, 03:12 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
This is a bash "feature", read is run by a subshell so your variable is set but lost immediatly.
Your example works under ksh though.
 
Old 07-28-2004, 06:01 PM   #3
kc8tbe
Member
 
Registered: Feb 2003
Location: Cleveland, Ohio (USA)
Distribution: Gentoo, Kubuntu 6.06
Posts: 179

Original Poster
Rep: Reputation: 30
The variable is still "lost" if the entire procedure occurs within a shell script instead of being run line by line by the user.

Any idea how to make bash not "lose" my variable? Using ksh exclusively for this purpose seems like a poor solution. (Although I might just switch to ksh altogether...)
 
Old 07-28-2004, 06:51 PM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi kc8tbe,
The common way to get this content would be
Code:
STRING=`echo "wassup"`
.
If you need to pipe the result of an operation, you can do it by
Code:
echo "wassup" |
while read STRING
do
  echo $STRING
done
 
Old 07-28-2004, 07:12 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You can also workaround this issue like this:
Code:
echo "wassup" |
(
read STRING
echo $STRING
...
)
Beware that after the block is done, the STRING variable is lost too.

Another annoying thing is that inside the block, you need to explicitly set the input if you want interaction with the user.
eg.
Code:
read reply < /dev/tty
Both problems applies to osvaldo's while loop too.
 
Old 07-29-2004, 01:46 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Maybe kc8tbe just want to echo "wassup" just after read the input
Code:
read STRING && echo "wassup"
echo $STRING
[edit]
After read more carefully, I feel my suggestion slighty out of lead
osvaldomarques seems to be right with STRING=`echo "wassup"`

Last edited by Cedrik; 07-29-2004 at 01:53 AM.
 
Old 07-29-2004, 06:45 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by kc8tbe
The variable is still "lost" if the entire procedure occurs within a shell script instead of being run line by line by the user.
I think you didn't understand it correctly when jlliagre said:
Quote:
This is a bash "feature", read is run by a subshell so your variable is set but lost immediatly.
In case of bash, "read" is not a program, but a builtin command. Then realize that a pipe ('|') connects the output of one process to the input of another. So, to have "read" read from a pipe there is another process needed. Since "read" is built inside bash, another bash-process is started to run "read". The reading of the variable occurs in a different process, which terminates inmediatly after running "read". When process isn't there anymore, all of its variables are lost.

This is no different from the command line or in a single script!

osvaldomarques' soltution is the "standard" way to read the output of a command (either builtin, or a seperate program or process):
Code:
# The classic syntax
STRING=`echo "wassup"`

# newer, bash's syntax (less comaptible, but has some advantages)
STRING=$(echo "wassup")
In this simple case of assigning a string to a variable, it's a "detour" to do this. To assign a string to a variable, you wou'd normally do:
Code:
STRING="wassup"
.

If one these doesn't do what you need for your specific purpose, please explain what it is you are trying to get done by the script.
 
Old 07-29-2004, 07:48 AM   #8
kc8tbe
Member
 
Registered: Feb 2003
Location: Cleveland, Ohio (USA)
Distribution: Gentoo, Kubuntu 6.06
Posts: 179

Original Poster
Rep: Reputation: 30
SOLVED

Thanks everyone! I had completely forgotten about the backtick (`). STRING = `echo "wassup"` does what I want it to.

For the curious, I'm writing a script to update /etc/hosts. Here's an excerpt:
Code:
#!/bin/sh
# Get an ip address for the domain name specified on the commandline.
IPADDR=`host $1 | egrep -o "(([0-9]{3}|[0-9]{2}|[0-9]{1})(\.?)){4}"`
# Do something useful with the $IPADDR (i.e. write it to a file).
# ...
# echo $IPADDR for the world to see,
echo $IPADDR
 
  


Reply



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
fsck.ext3: Attempt to read block from filesystem trouble dolphans1 Mandriva 12 10-07-2009 04:30 AM
What is "read manifest failed: Success"? (RH 7.3, trouble installing RPMs) vfoo Linux - Software 1 07-06-2005 10:56 AM
Trouble with bash read command Bebo Programming 5 05-21-2004 08:21 AM
trouble with twinview and 2 monitors, please read nappy Linux - Software 1 03-15-2004 06:42 AM
Trouble bootine SuSE 8.2 with ReiserFS in Read/Write mode alan8373 Linux - General 2 03-07-2004 12:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:44 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
Open Source Consulting | Domain Registration