ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
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" :-)
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.