Sleep then start over from beginning of script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sleep then start over from beginning of script
# 1  
Old 05-17-2016
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:

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

Last edited by nms; 05-17-2016 at 01:00 PM..
# 2  
Old 05-17-2016
How about:

Code:
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?
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-17-2016
Hi

Here's a sample:

Code:
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
# 4  
Old 05-17-2016
You can handle the file with one awk like this:

Code:
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.
# 5  
Old 05-18-2016
Ho Corona668,

What's the term
Code:
ORS=" " '

used for?
# 6  
Old 05-18-2016
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.

Last edited by RudiC; 05-18-2016 at 11:26 AM..
This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-18-2016
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to make the script jump to sleep?

Hi Gurus, I am working one below script. I am stuck at the else condition. #!/bin/ksh while : do cur_date=`date |cut -d"," -f 1` cur_time=`date +"%H%M" if ||; then if && ; then echo "File " $FIRST_FILE "... (8 Replies)
Discussion started by: ken6503
8 Replies

2. Shell Programming and Scripting

Remove certain lines from file based on start of line except beginning and ending

Hi, I have multiple large files which consist of the below format: I am trying to write an awk or sed script to remove all occurrences of the 00 record except the first and remove all of the 80 records except the last one. Any help would be greatly appreciated. (10 Replies)
Discussion started by: nwalsh88
10 Replies

3. Shell Programming and Scripting

Alternate to SLEEP for EXPECT within BASH script?

Fairly new to the System Admin world, and this is my first post here, hoping to get some clarification. I am using a BASH script to automate some Logfile Archiving (into .tars). The actual logfiles are accessed through an SSH, so I have used the following EXPECT sub-script within my main BASH... (8 Replies)
Discussion started by: Goatfarmer03
8 Replies

4. Shell Programming and Scripting

How to make the script to sleep for 30 min..?

Hi I have a script 'A' after it runs i want to sleep this for 30 min, after 30 min it should re run. How do i do that ..! thanks in advance (1 Reply)
Discussion started by: happydays
1 Replies

5. Shell Programming and Scripting

Kill a Script based on the pid and sleep

I would want to run a code for 1 min and if it doesnt succeed in 1 min..I would want to exit it..I am using the following code...But the script is not going into my code part.It is waiting for 60 secs and then getting killed. The code which is in the while loop actually takes less than 60 secs...... (6 Replies)
Discussion started by: infernalhell
6 Replies

6. Shell Programming and Scripting

Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way: How many times have you used the... (5 Replies)
Discussion started by: deckard
5 Replies

7. Shell Programming and Scripting

Test for shell interpreter at beginning of script

What would be the best way or method to determine or test for the shell interpreter at the beginning of a script in the event one shell is not available? If I use the following: #!/bin/bash and /bin/bash is not available, then use I'd like to use /bin/ksh if it is available. #!/bin/ksh (8 Replies)
Discussion started by: nck
8 Replies

8. Debian

Laptop waking up in bag: go back to sleep script

I have a Debian-derived distro (Kubuntu 8.04) running on a Dell Inspiron laptop. This laptop often wakes from RAM suspension while in my bag. I would like to run a script, upon RAM suspension wake, that would detect if the lid switch is closed. If so, then the script should put the machine back in... (0 Replies)
Discussion started by: dotancohen
0 Replies

9. Shell Programming and Scripting

how to start a process and make it sleep for 5 mins and then kill that process

how to start a process and make it sleep for 5 mins and then kill that process (6 Replies)
Discussion started by: shrao
6 Replies

10. Shell Programming and Scripting

Calling subscript but sleep halts the main script

Ok, I have written a main script which checks a directory contents every 30 secs then sleeps. The subscript does a usermod, if the user is logged on, it sleeps for 30 secs and then trys again over and over again. Here's the problem. when the subscript is called ./subscript.sh or exec... (1 Reply)
Discussion started by: doublejz
1 Replies
Login or Register to Ask a Question