⇤ ← Revision 1 as of 2007-10-24 13:50:32
Size: 1389
Comment: igli: initial example
|
Size: 1402
Comment: igli: minor ui
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
ProcessSubstitution 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: | 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: |
Line 29: | Line 29: |
Process substitution where the external is an awk command, is particularly powerful and flexible. | Process substitution where the external is an {{{awk}}} command, is particularly powerful and flexible. |
Line 32: | Line 32: |
Process substitution is definitely not portable. | Process substitution is definitely '''not''' portable. |
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=${l#*'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.