LinuxQuestions.org
Visit Jeremy's Blog.
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 08-12-2010, 08:48 PM   #1
roybal
LQ Newbie
 
Registered: Sep 2009
Posts: 8
Blog Entries: 8

Rep: Reputation: 0
capture the stdout of a heredoc in a variable


Why doesn't this work?
Code:
cat | myvar=$(</dev/stdin) <<EOS
  This is some content
EOS

echo "$myvar"
i have also tried several variants of cat in place of the redirection.
Code:
$(cat -) 
$(cat)
$(cat /dev/stdin)
None of the variants print "This is some content"

I have a version that works:
Code:
myvar=$(cat <<EOS
  This is some content
EOS
)

echo "$myvar"
but i don't like the syntax and it doesn't play nice in my editor with code folding.

Thanks!
 
Old 08-12-2010, 09:15 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
what is it that you are actually trying to do ? If you just want to store multiline text into a variable, then just assign to the variable

Code:
var="this 
is my 
comment"
 
Old 08-12-2010, 09:22 PM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@ghostdog74 He probably wants to redirect all output to a variable like a buffer stream in C++. Maybe it can work multiline on arrays with while loops,.. but not against a single variable.

You can do this:
Code:
cat <<< "$VARIABLE"
but not anything like this:
Code:
cat >>> VARIABLE
 
Old 08-13-2010, 07:52 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The reason it does not work is that each command in a pipeline is run in a subshell. The variable is being set as intended but in the subshell, so has no effect in the script.
 
Old 08-13-2010, 09:20 AM   #5
roybal
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Original Poster
Blog Entries: 8

Rep: Reputation: 0
Post Wow! 3 responses and 3 actually informative opinions

@qhost: Yes, this simple assignment works as far as the example I gave. Please excuse me for not providing more context, but I thought it would confuse the issue. @konsole is correct. What I am trying to accomplish is something like a data stream. @cat I see what you mean. So what can i use that stays in the same process space, takes stdin, and makes the assignment? Is there some form of redirection that will get me there? imaginary code: (<cat -)

Thank you guys
 
Old 08-13-2010, 08:23 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
For that concept you can still do something like this but it will be terrible I think. Only for bash version > 3.1 since I chose +=.

Code:
function readstream {
    __=''
    local REPLY IFS=''
    while read -n 1 -t 1 -d ''; do
        __+=$__$REPLY
    done
}

readstream < <(cat [args])

# do something with $__
Sometimes reading single characters works with -d '' only but sometimes it only works with IFS=''. The timeout is there just to make sure but it's not really used.

I guess I was wrong. It can also work with single (i meant normal) variables. I didn't think about +=. VAR=$VAR$NEWVALUE can also be used but that's already way too many malloc() and memcpy()/strcpy().

Last edited by konsolebox; 08-13-2010 at 08:50 PM.
 
  


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
Trying to capture STDOUT with PERL DBelcher Programming 2 10-16-2008 11:33 PM
python: how do you capture stdout stream from external program? BrianK Programming 8 01-24-2008 08:32 PM
How to capture both stdin and stdout of a child process? jineshkj Linux - Software 2 04-21-2006 06:34 AM
stdout contents capture arvind_tyche Programming 1 06-24-2004 06:08 AM
stdout capture anb bring to file sqn Programming 2 07-10-2003 07:42 AM

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

All times are GMT -5. The time now is 10:27 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