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

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -