LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-16-2009, 03:25 PM   #1
michiel.vandenbosch
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Rep: Reputation: 0
While input


Hi there!

So, I have a while -loop. And I stay in this loop unill I have reached the end of the file (result.txt). This file is the output of the "tree" command.

Now is my question: is it possible to directly insert the lines of the "tree" command in the loop, so I can skip the making of a file. Probably it has something to do with the line "done < result.txt"

Thanks for helping..


function overvieuwFunction {
while read line;
do
echo "${line}";
done < result.txt;
}

PROJECTDIR=`pwd`;
cd /;
rm $PROJECTDIR/result.txt;
tree -if -P "*.conf" $PROJECTDIR > $PROJECTDIR/result.txt;
cd $PROJECTDIR;

overvieuwFunction;
 
Old 02-16-2009, 03:29 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can skip the redirection to a file piping the output of the tree command directly to the while loop:
Code:
tree -if -P "*.conf" $PROJECTDIR | while read line
do
  echo "${line}"
done
 
Old 02-16-2009, 03:58 PM   #3
michiel.vandenbosch
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks, but this I have already tried. This works, but is not what I am searching. I don't know how to explain it well, but I will do it by example: I use this kind of code:



function countFunction {
while read line;
do
let $1=400;
done < result.txt;
}

LVAR=0;
countFunction LVAR;



And using the pipe, like in your example, makes that "let $1=400" does not work. Beceause, it has something to do that a pipe opens a new enviroment, so by doing this, the reference to LVAR (with the $1) is gone!

I hope you can follow :-)

So I realy have to find a solution for "done < result.txt" :-)

Sorry and greetings!
 
Old 02-16-2009, 07:30 PM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
I couldn't find a way to make bash do what you wanted without using a file in one way or another.

If you don't mind using a more traditional shell, ksh doesn't suffer from that 'pipelines in a subshell' nonsense that can be so annoying in bash.

Code:
#!/bin/ksh

function countFunction {


echo -en "line1\nline2\n" | while read line
do
  let $1=400
  eval echo 'in loop value of $1 is ' \$$1
done

eval echo 'in function after loop value of $1 is ' \$$1

}

LVAR=0
countFunction LVAR
echo $LVAR
By the way, passing the variable name into the function via $1 and then substituting that on the lefthand side of the assignment within the function is not a technique I've seen before and it's a little on the ugly side. It's not something I'd recommend doing if you can avoid it. It took me a good couple of minutes just to figure out what you were up to there.

Anyway, hope this was of some use to you.
 
Old 02-17-2009, 12:12 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I got the point. In bash an alternative is process substitution. It does not open a subshell as the pipe does and let you to use the output of a process (or multiple processes) as input to another process. For example:
Code:
while read line
do
  echo "${line}"
done < <(tree -if -P "*.conf" $PROJECTDIR)
In this way every variable assigned within the while loop is kept outside.
 
Old 02-17-2009, 03:37 AM   #6
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
Quote:
Originally Posted by colucix View Post
Code:
while read line
do
  echo "${line}"
done < <(tree -if -P "*.conf" $PROJECTDIR)
D'oh! I looked at process substitution, but as it substitutes to a filename I couldn't see how to use it on a while/done. It never occurred to me that it could be used with a redirection operator. (Well, it was about 1am in the morning!... Er, yeah, that excuse might fly! )

Nice one colucix, you've taught me something again.
 
Old 02-17-2009, 04:26 PM   #7
michiel.vandenbosch
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Yes, thanks, that was what I was searching!

Thanks a lot! Greetings from little cold Belgium!
 
  


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
HAL vs Xserver (/dev/input/mice?): convincing X to *not* use a given input device? stuartnlevy Linux - Desktop 6 01-24-2012 04:06 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
HELP connecting MIC Input/Line Input with Jack and Qsynth animehair Linux - Software 0 07-28-2006 07:47 AM
Ctrl+Shift Unicode input gone, after installing Japanese Input Methodes polemon Linux - Newbie 1 09-20-2005 05:17 PM
Sendmail: timeout waiting for input from local during Draining Input andrewstr Linux - Software 0 07-14-2004 01:43 PM

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

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