[arch-general] Start a daemon, show a syntax error

Mantas Mikulėnas grawity at gmail.com
Fri Feb 17 12:00:05 EST 2012


On 2012-02-17 18:07, Sebastian Schwarz wrote:
> On 2012-02-17 at 12:03 +0100, Lukas Fleischer wrote:
>> "done < $(foo)" isn't the same thing as "done < <(foo)".
> 
> Just out of curiosity: in unmount_all() in /etc/rc.d/functions
> this is used:
> 
>     while read -r target fstype options; do
>         ...
>     done < <(findmnt -mrunRo TARGET,FSTYPE,OPTIONS /)
> 
> But why not simply use a pipe in this case:
> 
>     findmnt -mrunRo TARGET,FSTYPE,OPTIONS / \
>             | while read -r target fstype options; do
>         ...
>     done
> 
> This should do the same and would be more idiomatic.  The commit
> message[1] when this was introduced did not say anything about
> that.
> 
> I wonder what might be the reason for this, understand it and
> thus improve my shell scripting skills. :)

Commands in a pipe would be executed using subshells, so any changes the
'while' loop makes to variables would be lost when the subshell exited.

In the current code, 'while' is executed in the main shell process (with
`findmnt` being inside a subshell), allowing the $mounts array to be
built inside the loop and used outside it.

(Play around with `while read; do echo $$ $BASHPID; done` in both forms
to see the primary difference.)

-- 
Mantas Mikulėnas <grawity at gmail.com>


More information about the arch-general mailing list