How can I use an conditional pipe filter in bash? -
i have filter want enable optionally , wonder how can in bash in clean way.
filter="| sort" # can empty ls $filter | cat
this code not work because call ls
|
, sort
parameters.
how can correctly? please mind trying avoid creating if
blocks in order keep code easy maintain (my piping chain considerable more complex example)
what have in mind doesn't work because variable expansion happens after syntax has been parsed.
you can this:
foo_cmd | if [ "$order" = "desc" ] ; sort -r else sort fi | if [ "$cut" = "on" ] ; cut -f1 else cat fi
Comments
Post a Comment