Zsh and bc, 29 bytes
[ "$1" = google ];bc<<<$#1/$?
Sample output:
$ zsh not_google.sh oogle
5
$ zsh not_google.sh google
Runtime error (func=(main), adr=5): Divide by zero
Explanation:
The first command exits true (zero) when the argument equals "google" and false (one) otherwise. That exit code is stored in $?
until another command completes, allowing us to divide by that number.
We then run bc
(with input via herestring) to compute the length of the argument divided by that exit code. This is either length ÷ 1 (the length) or length ÷ 0 (an error).
An un-golfed version:
if [ "$1" = google ]; then
divisor=0
else
divisor=1
fi
length="${#1}"
echo "$length / $divisor" |bc
Bash and bc, 31 bytes
This uses the same logic, but bash
can't handle $#1
without braces:
[ "$1" = google ];bc<<<${#1}/$?
Zsh and bc, 26 bytes (also errors on empty string)
This version also has an error when given an empty string, so I assume it doesn't count. It has the exact same output as the previous version (except when given an empty string or no argument).
bc<<<$#1/(0!=${#1#google})
Un-golfed version of 26 byte answer:
length="${#1}" # the length of the first argument
unprefix_google="${1#google}" # google → "", googler → r, goog → goog, "" → ""
length_unprefix_google="${#unprefix_google}"
if [ 0 != "$length_unprefix_google" ]; then
divisor=1 # $unprefix_google is an empty string
else
divisor=0 # $unprefix_google is not empty
fi
echo "$length / $divisor" |bc