Sleep till grep find the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sleep till grep find the string
# 1  
Old 07-05-2016
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:

Code:
#! /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
# 2  
Old 07-05-2016
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...

Code:
#! /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

This User Gave Thanks to elbrand For This Post:
# 3  
Old 07-05-2016
Try:

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

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

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 07-05-2016
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep to find an exact string

Hi all, I tried searching the forum for this, and I read numerous suggestions here and even on other forums, and I cannot get this to want the way that I need it to. I tried grep -W / -f to no luck. Here is what I have. I have a list of file names- FILE1-FILE1TEST,FILE1RELATION... (7 Replies)
Discussion started by: jeffs42885
7 Replies

2. Shell Programming and Scripting

Grep command to find string in Variable in file2

Hi , I am executing 2 queries and output is saved in file1.txt and file2.txt example of file1.txt Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,, Testing Excel _.xlsx,/Lab/Development and... (3 Replies)
Discussion started by: Sunil Mathapati
3 Replies

3. Shell Programming and Scripting

Grep with loop till search is done

I need help to put a script where it runs the svn command grep'ing for the ticket# in the comments to see if the ticket was used in the latest commit. so on command line: ./test.sh ticket-1 ticket-2 ticket-3 It should be able to check if ticket-1 is used first and if not then check if... (2 Replies)
Discussion started by: iaav
2 Replies

4. UNIX for Dummies Questions & Answers

Using grep to find files that don't contain a string

Hi all, I am still learning my way around unix commands and I have the following question. I have a website and I want to search for all the html pages that don't contain a certain js file. The file I am searching for is located under /topfolder/js/rules.js . So I assume in my grep search I... (5 Replies)
Discussion started by: SyphaX
5 Replies

5. Shell Programming and Scripting

extract till occurance of a string

hi , i have an xml that comes in a single, the entire xml file is read as a single line when i open in edit plus or unix. i need to amend the contents of this xml file. below is the extract from the file <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"... (5 Replies)
Discussion started by: sais
5 Replies

6. UNIX for Dummies Questions & Answers

grep string and then find another string

I have a file that looks like this: Name=TOM abcded asdfas fkoiaerj inadhah Name=Chris 23nafds vasdkfna afadns afdadsfa aaaaaa bbbbbb cccccc I would to search for the string 'bbbbbb', then I would like to search above that string for the 1st occurrence of "Name" and not the other... (7 Replies)
Discussion started by: doza22
7 Replies

7. Shell Programming and Scripting

find exarct string using grep

Hi i have t1 file like: HiPerformance: Recalculation Dates|234| HiPerformance: Recalculation|456| ..... ..... .... ... i wrote grep command like: grep "HiPerformance: Recalculation" t1 out put: HiPerformance: Recalculation Dates|234| HiPerformance: Recalculation|456| But i... (3 Replies)
Discussion started by: koti_rama
3 Replies

8. UNIX for Dummies Questions & Answers

Loop till you find a string in a fine <-- Need Help New to Unix Scripting

Guys - I am new to Unix scripting and am in need for a script that does the following. I have bits and pieces created and tested but i am just having a little difficult time getting it all together. - Loop through till it finds a string in a specific file. Any help is greatly appreciated. ... (1 Reply)
Discussion started by: mrehman
1 Replies

9. Shell Programming and Scripting

grep string and find line before

hi, i have to grep for string in file but i want to find the group of this line so i must get lines before and select the group. the file look like : ####name_groupe1 alphanumeric line alphanumeric line .. ####name_groupe2 alphanumeric line alphanumeric line .. ####name_groupe3... (4 Replies)
Discussion started by: kamel.seg
4 Replies

10. Shell Programming and Scripting

How to replace all string instances found by find+grep

Hello all Im performing find + grep operation that looks like this : find . -name "*.dsp" | xargs grep -on Project.lib | grep -v ':0' and I like to add to this one liner the possibility to replace the string " Project.lib" that found ( more then once in file ) with "Example.lib" how can I do... (0 Replies)
Discussion started by: umen
0 Replies
Login or Register to Ask a Question