how to get the string stored in a variable in a line???

Hi all,

I want to search for a data type in a line.For this in a loop i am checking for $DATA_TYPE in a line using grep.But grep is not able to find when i give this.
Can any one tell me how to check string in $DATA_TYPE variable in line usign grep (or) any other way to do the above task.

declare -a TYPES_LIST
declare -a EXIST_FLAG=0
TYPES_LIST[0]="char"
TYPES_LIST[1]="int"
TYPES_LIST[2]="float"
TYPES_LIST[3]="double"

for DATA_TYPE in "${TYPES_LIST[@]}"
do

            EXIST_FLAG=\`echo $CHECK_LINE|grep -c ':$[ *]"$DATA_TYPE"'

            if test $EXIST_FLAG -gt 0 ; then
                    echo $CHECK_LINE
                    break
            fi
    done

Here CHECK_LINE="580:$ char *tablename;"

Thanks in Advance
JS

To parse the line you can do something like this:

for DATA_TYPE in "${TYPES_LIST[@]}"
do
  echo "$CHECK_LINE"|grep $DATA_TYPE >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    echo "Find in "$CHECK_LINE" datatype = "$DATA_TYPE
    break
  fi
done

Regards

The problem with your script is the use of a literal dollar sign in the regular expression; this is a special character, which means "end of line" to grep. You can escape it by prefixing it with a backslash, or (like you alrady did with the asterisk) by putting it in a character class. Also you seem to be missing a closing single quote.

The motions to run grep in backticks and then examine the output are redundant, you can just run [ef]grep in the if directly.

grep cannot (typically) grep for multiple things, but you can look at egrep or fgrep if you want to simplify your script.

if echo "$CHECK_LINE" | egrep ':[$][ ][*](char|int|float|double)'; then
  break
fi

grep (without -c or -q) prints the matching line as a matter of course, so you don't need to do that separately.

Even the break (and thus the whole if, and the whole for loop) is redundant if you use egrep or fgrep to look for all the possibilities in one go.

Franklin52: et tu Brute. Please don't perpetrate this "if test $?" idiom when if already checks the value of $? by design.

Replace your entire script with:

case $CHECK_LINE in
  *char*|*int*|*float*|*double*) printf "%s\n" "$CHECK_LINE ;;
esac

If you need to know which type was found:

case $CHECK_LINE in
  *char*)   type=char ;;
  *int*)    type=int;;
  *float*)  type=float;;
  *double*) type=double;;
  *) type= ;;
esac
if [ -n "$type" ]
then
     printf  "%s\n" "$CHECK_LINE
fi