[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] How to figure out the dev path of process substitutions?
From: |
Stephane Chazelas |
Subject: |
Re: [Help-bash] How to figure out the dev path of process substitutions? |
Date: |
Sat, 21 Sep 2019 09:13:16 +0100 |
User-agent: |
NeoMutt/20171215 |
2019-09-20 17:33:21 -0500, Peng Yu:
> $ grep x -H -n <(printf '%s' {1..3}) <(printf '%s' {a..c}) <(printf '%s'
> {x..z})
> /dev/fd/61:1:xyz
>
> I'd like to figure about of the dev path of process substitutions in
> the command like the above. So that I can post process the result as
> in a pipeline.
[...]
Process substitution expands to that dev path. I think you're
thinking it's more complicated than it is. <(cmd) expands to
the name of a file which contains the output of cmd, that's all
there is to it.
If you want to reuse the name of the file to post process the
output, you'd do it like with any other file.
Like:
search_and_annotate() (
# assumes file names don't contain @, newline or RE operator.
# If that can't be guaranteed, this code amounts to an
# arbitrary command injection vulnerability with GNU sed.
regex=$1; shift
annotate_sed_script=
n=1; for file do
annotate_sed_script="$annotate_sed_script
s@^${file}:@ARG${n}:@;t"
n=$((n + 1))
done
grep -e "$regex" -- "$@" | sed -e "$annotate_sed_script"
)
search_and_annotate x <(printf '%s\n' {1..3}) \
<(printf '%s\n' {a..c}) \
<(printf '%s\n' {x..z})
Gives:
ARG3:1:x
--
Stephane