Help on the regular expresion =~

my $hw_plf_desc = `grep hw_platform $NODE_CFG_FILE`;
  if($hw_plf_desc =~ /Netra X4270 X4446A M2 /)

Could someone explain the use of =~ .... this works only for perl . What is the alternate for the same in shell . Could any one convert this to shell script

=~ is regex binding operator
More about it here.

#!/bin/bash
hw_plf_desc=$(grep hw_platform $NODE_CFG_FILE)
if [ "X$(echo $hw_plf_desc  | grep 'Netra X4270 X4446A M2')" == "X" ] ; then
   echo "Not a Nextra X4270 X4446A M2"
else 
   echo "Woohoo it's a Nextra X4270 X4446A M2"
fi
1 Like

Equivalent in shell scripting would be something like this

 
 if [$hw_plf_desc -eq [Netra X4270 X4446A M2] ]
then
...
..
fi

for string comparision ...its always good to use == :slight_smile: ....

eg:
v=haiif [ $v == "Netra X4270 X4446A M2" ];then
echo "compare hai"
else
echo "not matching"
fi
not matching

e.g.

ds=`grep "Netra X4270 X4446A M2" tmpfile`
"$ds" = "[Netra X4270 X4446A M2]" ]; echo "Match"

o.p
Match

Skrynesaver

It works fine .

Could you please explain the capital x ( X) does

If the grep comes back empty it means that you are comparing "X" with "X" rather than a blank with a pattern, it's a hang over in my style from shells that fell over if there was an empty string on one side of a comparison

1 Like