assigning command output to a shell variable

I have the sql file cde.sql with the below contents:

abcdefghij

abcwhendefothers
sdfghj
when no one else
when others
wwhen%others
exception when others

Now I want to search for the strings containing when others together and ceck whether that does not occur more than once in the file.

I have a shell script file sample1 as below:

#!/bin/sh

x=$(basename $1)
y=$(dirname $1)
echo "filename: $x"
echo "Dir name: $y"
file_ext=${x##*.}
if [ ! -f $x ]; then
printf "File $x is not a valid file"
exit
fi
if [ $file_ext = sql ] || [ $file_ext = pls ] || [ $file_ext = xml ]; then
printf "This file has extension $file_ext\n";
grep -ciw 'when others' $x>when_others_exception

fi

Here in the if loop i have found the occurrence of when others using grep command.

As of nw, i had written the o/p of that command to a file "when_others_exception".

How can i assign the o/p of the grep command to any shell variable.

I searched a lot on the web n documents and unable to find how do we ssign the o/p of any shell command to a shell variable.

Doesn't this a very bad coding.

Can any one tell me how do we assign o/p of any shell command to a shell variable.

And also here i ahd written the contents to a file "when_others_exception".
Can any one tell me how do we read that value and compare with any numeral.

Thanks,
Kiran.

#!/bin/ksh

a=$(date)
echo "myDate->[${a}]"

Thanx, So inorder to capture the o/p for any command we should be using $ and the command in brackets(()).

Thanks