Use conditional operators and grep -q flag to shorten function