I want to run a retry function that accepts as arguments an operator and a cmd in order to reuse the function.. e.g.
"-z" and "pgrep -f 'sleep 10'"
"! -z" and "pgrep -f 'sleep 10'"
#!/bin/bash
retry() {
local -r operator="$1"; shift
local -r cmd="$@"
retry_count=5
attempts=0
echo "run $cmd"
#until [ $operator `pgrep -f 'sleep 10'` ] <---this works
until [ $operator `${cmd}` ]
do
sleep $((++attempts))
if [ $attempts -gt $retry_count ]; then
echo "failed. exit with error"
exit 1
fi
echo "try again ..."
done
echo "success"
}
retry "! -z" "pgrep -x 'sleep 10'"
retry "-z" "pgrep sleep"
I got this error message:
pgrep: only one pattern can be provided
Try `pgrep --help' for more information.
I am open to use a different approach.