Re: Start a daemon, show a syntax error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



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@xxxxxxxxx>


[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]
  Powered by Linux