Richard Rublev Richard Rublev - 3 months ago 9
Bash Question

What is wrong with my pattern matching?

Files in my directory

262_V01_C00_R000_TEx_BL_2048H.dat 262_V01_C02_R000_THx_BL_2048H.dat
262_V01_C01_R000_TEy_BL_2048H.dat 262_V01_C03_R000_THy_BL_2048H.dat


My script

#!/bin/bash

for i in *.dat;
do
if [ "$i" == "$TEx" ];then
file1= "$i"
fi
if [ "$i" == "$TEy" ];then
file2= "$i"
fi
done

echo file1
echo file2


I got

file1
file2


My aim was

file1=262_V01_C00_R000_TEx_BL_2048H.dat
file2=262_V01_C00_R000_TEy_BL_2048H.dat

Answer

Try:

  • [[ "$i" =~ TEx ]] instead of [ "$i" == "$TEx" ].

  • file1=$i, without spaces around =. Also, the double quotes are not necessary.

  • echo file1=$file1 not just echo file1.

Comments