Bash script

Hi

i read a file row by row and cut into fields.
and store the content of fields to a variable.

when i use the variable in if condition it giving me error.
the error is :

 integer expression expected[:

storing it in the variable like this

  Tval=`echo ${line} | cut -d "#" -f 2`

and using the variable in if condition like this

if [ $Tval -eq $counter ] 
then
  echo "No of rows matched with T Value" 
else 
  echo "Not matched" 
fi

thanks

Have you initialize the counter variable ?

what is the value of $line?
one of the variable from $Tval and $counter, is probably null value or not integer.

How about this

line='hi#how#are#you'
  Tval=`echo ${line} | cut -d "#" -f 2`

  counter="how" # defining the value is must 
if [ $Tval = $counter ]  # we can compare string using = only 
then
  echo "No of rows matched with T Value"
else
  echo "Not matched"
fi

Change the -eq with =

 
if [ $Tval = $counter ] 
then
  echo "No of rows matched with T Value" 
else 
  echo "Not matched" 
fi

As we use -eq for numeric and = for string test
Check man page of test for more details

line='hi#111#are#you'
  Tval=`echo ${line} | cut -d "#" -f 2`

  
if [ $Tval = $counter ]  then
  echo "No of rows matched with T Value"
else
  echo "Not matched"
fi

i cut the field and store in the variable Tval.the Tval contains a string.

i want to convert it integer. because i want to use in if condiotion the other operator($counter ) is interger.

Thanks

$Tval contains '111' which is automatically treated as integer.
if $counter is an integer then the condition is valid.

echo "tval is $Tval"
echo "counter is $counter"
if [ "$Tval" -eq  "$counter" ] then
echo "No of rows matched with T Value"
else
echo "Not matched"
fi

check what is the value of the variables.