0

I want to compare file name string with file mask in a ksh shell script. Can you please correct the syntax?

if [ "NPER20200422TEST.PTN" = "NPER*.PTN" ]
then
    echo "File matched"
else
    echo "File not matched"
fi

I want to match my file string with given file mask.

0

3 Answers 3

4

make sure you're using or or , then

if [[ "NPER20200422TEST.PTN" == NPER*.PTN ]]

Use the double bracket conditional, and the special globbing metacharacters must be unquoted.

0
2

In Bourne-like shells including ksh, that's what the case construct is for:

case NPER20200422TEST.PTN in
  (NPER*.PTN) echo "File matched";;
  (*) echo "File not matched";;
esac

Note that in the Bourne shell (a historical shell that is no longer in use today, though many people keep confusing sh with it), you had to omit the (s. Either (*) or *) are allowed in standard (POSIX) implementations of sh.

-1

You can print your statement with an echo and pipe it to the grep:

if echo NPER20200422TEST.PTN | grep -q "NPER.*PTN"
then
  echo "File matched"
else
  echo "File not matched"
fi

grep -q will not produce an output

1
  • That would also match on 'fooNPERbarPTNbaz' and fail to match on $'NPER\n.PTN' though. Aug 21, 2021 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .