add value to array in loop bash
Greetings
i have a loop as follows: 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 |
hi i closed the other threads i had open. thanks.
|
Using the | creates pipe and a subshell as grail explained use redirection instead eg
Code:
var=1 |
Or just ditch cat altogether :)
Code:
while |
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 |
Quote:
Code:
... 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. |
All times are GMT -5. The time now is 08:56 AM. |