test=$line i=0
while case "$test" in (*select*)
test=${test#*select};;(*) ! :;;
esac; do i=$(($i+1)); done
You don't need to call grep
for such a simple thing.
Or as a function:
occur() while case "$1" in (*"$2"*) set -- \
"${1#*"$2"}" "$2" "${3:-0}" "$((${4:-0}+1))";;
(*) return "$((${4:-0}<${3:-1}))";;esac
do : "${_occur:+$((_occur=$4))}";done
It takes 2 or 3 args. Providing any more than that will skew its results. You can use it like:
_occur=0; occur ... . 2 && echo "count: $_occur"
...which prints the occurrence count of .
in ...
if it occurs at least 2 times. Like this:
count: 3
If $_occur
is either empty or unset
when it is invoked then it will affect no shell variables at all and return
1 if "$2"
occurs in "$1"
fewer than "$3"
times. Or, if called with only two args, it will return
1 only if "$2"
is not in "$1"
. Else it returns 0.
And so, in its simplest form, you can do:
occur '' . && echo yay || echo shite
...which prints...
shite
...but...
occur . . && echo yay || echo shite
...will print...
yay
You might also write it a little differently and omit the quotes around $2
in both the (*"$2"*)
and "${1#*"$2"}"
statement. If you do that then you can use shell globs for matches like sh[io]te
for the match test.
...<<<"$line"
. The commandgrep
is expecting a file instead – Valentin Bajrami Oct 23 '14 at 15:33