If I have the text in a shell variable, say $a
:
a="The cat sat on the mat"
How can I search for "cat" and return 4 using a Linux shell script, or -1 if not found?
1、
With bash
a="The cat sat on the mat"
b=cat
strindex() {
x="${1%%$2*}"
[[ $x = $1 ]] && echo -1 || echo ${#x}
}
strindex "$a" "$b" # prints 4
strindex "$a" foo # prints -1