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
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?