Differences between revisions 2 and 3
Revision 2 as of 2007-10-24 13:52:37
Size: 1402
Editor: 82
Comment: igli: minor ui
Revision 3 as of 2007-10-24 13:54:22
Size: 1405
Editor: 82
Comment: igli: gah l should be line
Deletions are marked like this. Additions are marked like this.
Line 26: Line 26:
 'Note: the '*'secret plans are backed up at:'*) criticalFile=${l#*'secret plans are backed up at:'}  'Note: the '*'secret plans are backed up at:'*) criticalFile=${line#*'secret plans are backed up at:'}

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)