Sleep till grep find the string

Hello!

I have a sample code in which a grep is being performed from multiple files with a date format in their naming convention. Here the script:

#! /usr/bin/bash
 cd /IN/service_packages/SMS/cdr/received
 MYDATE=`date +"%Y%m%d%H%M"`
  
 #get the value then divide by 60
#DAPS_SLC01
data=`grep "CALL TYPE=D" cubic-live-slc-a_ACS_$MYDATE* | wc -l`
echo Data:
r=`echo "scale=3; $data/60" | bc | sed -e 's/^\./0./;s/[0]*$//g'`
echo DAPS_SLC-a $r
 #DAPS_SLC02
data1=`grep "CALL TYPE=D" cubic-live-slc-b_ACS_$MYDATE* | wc -l`
r=`echo "scale=3; $data1/60" | bc | sed -e 's/^\./0./;s/[0]*$//g'`
echo DAPS_SLC-b $r
 #DAPS Total
r=`echo "scale=3; ($data + $data1)/60" | bc | sed -e 's/^\./0./;s/[0]*$//g'`
echo DAPS_Total $r

Sometimes when the script is run, the files with that timestamp are not created yet, so of course the grep will fail. How can I input a sleep to wait till the files exist, then run the grep?

Many thanks

you could simply wait, till files with the name pattern occur. It could be useful to add a timeout to avoid your script to wait forever...

#! /usr/bin/bash
 cd /IN/service_packages/SMS/cdr/received
 MYDATE=`date +"%Y%m%d%H%M"`

while ! { find . -mindepth 1 -maxdepth 1 -name "cubic-live-slc-a_ACS_${MYDATE}*" | grep . ; } ; do sleep 10 ; done
  
 #get the value then divide by 60
#DAPS_SLC01
data=`grep "CALL TYPE=D" cubic-live-slc-a_ACS_$MYDATE* | wc -l`
echo Data:
r=`echo "scale=3; $data/60" | bc | sed -e 's/^\./0./;s/[0]*$//g'`
echo DAPS_SLC-a $r
 #DAPS_SLC02
data1=`grep "CALL TYPE=D" cubic-live-slc-b_ACS_$MYDATE* | wc -l`
r=`echo "scale=3; $data1/60" | bc | sed -e 's/^\./0./;s/[0]*$//g'`
echo DAPS_SLC-b $r
 #DAPS Total
r=`echo "scale=3; ($data + $data1)/60" | bc | sed -e 's/^\./0./;s/[0]*$//g'`
echo DAPS_Total $r
1 Like

Try:

exists() {
  [ -e "$1" ]
}

until exists cubic-live-slc-a_ACS_"$MYDATE"*
do
  sleep 1
done
2 Likes

Hi

Thanks elbrand. I used the min/maxdepth but the command did not work on my version (Solaris 11).
I then used Scrutinizer's until and it worked perfectly.
Many thanks to both for your input.