Differences between revisions 4 and 5
Revision 4 as of 2008-02-14 11:40:42
Size: 1402
Editor: MrIgli
Comment: minor: quoting fix
Revision 5 as of 2008-11-22 14:09:30
Size: 1402
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

Process Substitution

Process Substitution is a very useful BASH extension. It is similar to awk's "command" | getline and is especially important to get round subshell restrictions in pipes, eg:

hasFile='Note: the (top-|highly )?secret plans are backed up at:(.*)'
criticalFile=
while read -r line
do [[ $line ]] || continue
        case $line in
        '!!! '*) errMsg "${line#'!!! '}"
;;      *important* ) echo "$line"
;;      * ) if [[ $line =~ $hasFile ]]; then
                criticalFile=${BASH_REMATCH[2]}
                warn "File at $criticalFile"
            else spin
            fi
;;      esac
done < <(command $options "${param[@]}" 2>&1|tee "$logfile")
[[ $criticalFile ]] || abort 'File not found'

Piping the command to a while loop would mean any variables set would be lost. Note that the actual command can be a pipeline. In fact you can continue to type a whole script in that side as well. Be aware that this is running in a subshell, and also that it will continue to run when your script exits (unless you manage your child processes.)

In the above example the regex could as easily be done with a case:

        'Note: the '*'secret plans are backed up at:'*) criticalFile=${line#*'secret plans are backed up at:'}

Process substitution where the external is an awk command, is particularly powerful and flexible.

Portability

Process substitution is definitely not portable.

ProcessSubstitution (last edited 2016-09-22 12:53:39 by geirha)