I have this simple script, which does nothing more than:
- check if an email matches a specific pattern
- in that case, add a tag to a taglist
- before quitting, print that taglist
set -e
lista_tag=()
in_file="/tmp/grepmail-classify.txt"
# save stdin to file, to use it multiple times
cp /dev/stdin $in_file
# CLASSIFY
res=$(grepmail -B "some regex pattern" < $in_file)
if [ ! -z "$res" ]
then
lista_tag+=("PUSH")
fi
res=$(grepmail -B "some other regex pattern" < $in_file)
if [ ! -z "$res" ]
then
lista_tag+=("MERGIFY")
fi
# ⁝ Many many more similar patterns
# output them comma separated
echo ${lista_tag[*]}
As you can see there is a case for refactoring and abstraction. res and if .. fi parts are repeated. But I am not sure how to safely pass commands around. What I think I would like to do is to invoke a function like this (or similar):
classify '"grepmail -B "somepattern"' 'MYTAG'
But it is tricky! I have read the FAQ but I am not sure it covers my case.
So here is the question: what is the correct way to pass commands around (if there is any)? How would the res= part of such function look like?