Quantcast
Channel: User Zoredache - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 41

Iterate over the output of a command in bash without a subshell

$
0
0

I want to loop over the output of a command without creating a sub-shell or using a temporary file.

The initial version of of my script looked like this, but this doesn't work since it creates a subshell, and the exit command terminates the subshell instead of the main script which is required. It is part of a much larger script to configure policy routing, and it is halt the execution if it detects a condition that will cause routing to fail.

sysctl -a 2>/dev/null | grep '\.rp_filter' | while read -r -a RPSTAT ; do  if [[ "0" != "${RPSTAT[2]}" ]] ; then    echo >&2 "RP Filter must be disabled on all interfaces!"    echo >&2 "The RP filter feature is incompatible with policy routing"    exit 1  fidone

So one of the suggested alternatives is to use a command like this to avoid the subshell.

while read BLAH ; do echo $BLAH; done </root/regularfile

So it seems to me that I should also be able use a command like this to avoid the subshell and still get the output from the program I want.

while read BLAH ; do echo $BLAH; done <(sysctl -a 2>/dev/null | grep '\.rp_filter')

Unfortunately, using that command results in this error.

-bash: syntax error near unexpected token `<(sysct ...

I get really confused since this does work.

cat <(sysctl -a 2>/dev/null | grep '\.rp_filter')

I could save the output of that command to a temporary file, and use redirect the on the temporary file, but I wanted to avoid doing that.

So why is the redirection giving me an error, and do I have any options other then creating a temporary file?


Viewing all articles
Browse latest Browse all 41

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>