LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-22-2010, 11:37 AM   #1
theaceoffire
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Rep: Reputation: 0
Question Bash: How do I read data from a variable while that variable is being populated?


This is an attempt to clean out my scripts and to stop using temporary files to pass data.

I have two scripts: ScriptA (aka, "loudmouth"), and ScriptB (aka, "watcher"):

Code:
#!/bin/bash
#loudmouth.sh
i=1
while [ $i -lt 100 ];do
        echo "Look at me count!$i"
        sleep 1
        let "i=$i+1"
done
Code:
#!/bin/bash
#watcher.sh
oneline(){
        PROC=$1
        while [ -d /proc/$PROC ];do
                echo -en "\r" "`tail -n1 tmp.txt`"
        done
}

( eval $1 ) &>tmp.txt & oneline $!
And you activate my current example with:
Code:
>watcher.sh loudmouth.sh
I would like to have these same results without using tmp.txt AND without changing loudmouth... in other words, I would like to stream data into a variable, and randomly read out the last line of that variable.

^_^ SO, to recap:
I currently write to a file continuously, and randomly read the last line of that file to see how the process is progressing.

I would like to write to a variable continuously, and randomly read the most recent line of that variable to see how the progress is progressing.

Thanks!
 
Old 04-22-2010, 12:40 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
Not sure what reading "randomly" is.
Code:
#!/bin/bash
#watcher.sh

( eval $1 ) 2>&1 | while read LINE ; do
    echo -en "\r" "$LINE"
done
 
Old 04-22-2010, 09:22 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
@ntubski - I have heard of some issues with pipe being used this way hides this loop in another shell (or something like that)
Is it also possible to do it this way?

Code:
while read LINE; do
    echo -en "\r" "$LINE"
done < <(eval $1) 2>&1
Not a hundred percent on the formatting of the last part, so I am hoping you understand what i mean?
 
Old 04-23-2010, 01:09 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
Quote:
Originally Posted by grail View Post
@ntubski - I have heard of some issues with pipe being used this way hides this loop in another shell (or something like that)
Yes, every command in the pipeline gets its own subshell.


Code:
while read LINE; do
    echo -en "\r" "$LINE"
done < <(eval $1 2>&1)
Seems to work, I moved the parenthesis to avoid an annoying error message on Ctrl+C.
 
Old 04-23-2010, 02:29 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
This screams race condition.

BTW, do you know about the fairly ubiquitous utility "watch"? I believe it accomplishes what you want.
 
  


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] Read file's content into variable problem Coolmax Programming 6 08-31-2009 04:12 PM
Challenge in Bash script with read variable manya Programming 2 07-26-2009 11:00 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
bash read files to variable question babag Programming 7 06-25-2007 05:04 AM


All times are GMT -5. The time now is 12:35 PM.

Main Menu
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