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.
declare -a Array
var=1
cat ./some_file | while read line
do
some_command_where_a_value_is_returned
((var++))
done
i'd like to add the returned value to Array.
something like this below which isn't working. suggestions please? Thanks again array+=("some_command_where_a_value_is_returned")
Anything in a subshell is undone once returned to the parent shell (pipe into while is causing a subshell). Hence the array is once again empty.
Just a side note, you seem to have several questions on the go ... are you going to close any of the older ones?
Most seem related so it is hard to imagine you have moved on without solving the prior ones first. just a thought
Since often in such loops I have to do something with stdin/stdout, which is already used by your loop (and thus can cause unpredictable behaviour), I tend to use another channel:
Code:
while read -u 9 line
do
.... stuff
done 9<somefile
now you can do stuff with stdin and stdout as usual.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
Quote:
Originally Posted by grail
Or just ditch cat altogether
Code:
while
do
...
done<somefile
./ not required as the default is to look in the current directory.
Agreed I was just using the code the OP had already provided to demonstrate, also of course you can use any other command inside the <() construct not just cat, eg
Code:
...
done < <(top -bn1)
...
Which would send the output of a single 'top' run into the loop.
I just use './' as a matter of course just so it's obvious I am using a file/command in the current dir, just a habit and it does no harm.
Last edited by Keith Hedger; 01-28-2016 at 05:21 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.