Sleep then start over from beginning of script

Hi all,

I have a bash script in which it runs multiple if conditions if a word count from a grep is equal to a number. When this number is not reached I want to sleep for very few seconds (let's say 3) and start over again, till it matches the word count of 8.

Here's a sample of the code so far:

 #!/bin/bash
  
 filepath=/root/scripts/mfile/mfile_log.txt
 nooffiles=`grep rw $filepath | wc -l`
 if [ $nooffiles == 8 ]; then
  
 size=`grep rw $filepath | head -1 | awk '{print$5}' | sed -e 's/\r//g'`
name=`grep rw $filepath | head -1 | awk '{print$9}' | sed -e 's/\r//g'`
 size1=`grep rw $filepath | head -2 | tail -1 |awk '{print$5}' | sed -e 's/\r//g'`
name1=`grep rw $filepath | head -2 | tail -1 |awk '{print$9}' | sed -e 's/\r//g'`
 if [ $size == $size1 ] && [ $name == $name1 ]; then
echo WHMNZ_VWS_Mfiles SYNCHED
else
echo WHMNZ_VWS_Mfiles NOT_SYNCHED
fi

 else
 if [ $nooffiles != 8 ] then;
 SLEEP 3 AND STARTOVER...
 fi
 fi

How can I achieve the last part?
Thanks in advance

How about:

filepath=/root/scripts/mfile/mfile_log.txt

nooffiles=0

# Loop until no of files equals 8.
while [ $nooffiles -ne 8 ] # Not all shells support ==, !=, etc.  but everything should be ok with -ne
do
        nooffiles=$(grep -c rw $filepath) # grep can count without wc, via grep -c

        [ $nooffiles -ne 8 ] && sleep 3
done

 size=`grep rw $filepath | head -1 | awk '{print$5}' | sed -e 's/\r//g'`
name=`grep rw $filepath | head -1 | awk '{print$9}' | sed -e 's/\r//g'`
 size1=`grep rw $filepath | head -2 | tail -1 |awk '{print$5}' | sed -e 's/\r//g'`
name1=`grep rw $filepath | head -2 | tail -1 |awk '{print$9}' | sed -e 's/\r//g'`
 if [ $size == $size1 ] && [ $name == $name1 ]; then
echo WHMNZ_VWS_Mfiles SYNCHED
else
echo WHMNZ_VWS_Mfiles NOT_SYNCHED
fi

Also, there's quite probably a better way to get data from the file than 16 separate invocations of grep, head, awk, tail, sed, and awk. Could you show us what the data file looks like please?

1 Like

Hi

Here's a sample:

spawn ssh -o StrictHostKeyChecking=no root@172.21.94.26
Password:
Last login: Tue May 17 16:16:06 2016 from 172.21.95.1
Oracle Corporation      SunOS 5.11      11.3    September 2015
root@vws-a:~# ls -ltr /IN/service_packages/CCS/MFile | tail -1
-rw-r--r--   1 ccs_oper esg       163044 Apr 28 10:46 20160428104632
root@vws-a:~# spawn ssh -o StrictHostKeyChecking=no root@172.23.94.26
Password:
Last login: Tue May 17 16:16:10 2016 from 172.21.95.1
Oracle Corporation      SunOS 5.11      11.3    September 2015
root@vws-h:~# ls -ltr /IN/service_packages/CCS/MFile | tail -1
-rw-r--r--   1 ccs_oper esg       163044 Apr 28 10:46 20160428104632
root@vws-h:~# spawn ssh -o StrictHostKeyChecking=no root@10.192.1.20
Password:
Last login: Tue May 17 16:16:11 2016 from 10.192.4.15
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
You have new mail.
Sourcing /etc/profile.ORA
Sourcing //.profile-EIS.....
root@am1vws01 # ls -ltr /IN/service_packages/CCS/MFile | tail -1
-rw-r--r--   1 ccs_oper esg      63032068 May  8 22:39 20160508221053

These are multiple ssh connections done with expect. Then I simply grep for the term "rw" and use awk to get the size and name of the file

You can handle the file with one awk like this:

read SIZE1 NAME1 SIZE2 NAME2 G <<EOF
$(awk -v ORS=" " '/^-rw/ { sub(/\r/,""); print $5, $9 } END { printf("\n"); }' "$filepath")
EOF

awk will print something like 163044 20160428104632 163044 20160428104632 63032068 20160508221053 which read will get all in one go.

If you set up shared keys with these servers, you will no longer need to use the expect third-party brute forcing tool to automate them, either.

Ho Corona668,

What's the term

ORS=" " '

used for?

It's the "Output Record Separator" that by default would print <NL> (= '\n', 0x0A) chars; setting it to space will print everything in one line.

1 Like

It's also the reason I do printf("\n"); at the end, with RS changed something else has to print a newline when everything's done.