1820038 Members
3080 Online
109608 Solutions
New Discussion юеВ

script

 
SOLVED
Go to solution
navin
Super Advisor

script

Hello All,
I have to write a shell script for the below algorithm. Any assistance..?
Surely appreciated. Thanks
1.execute: a command
2.after execution completes, read job_1000345.log and look for a message "Uptodate"
3.if the message is not found, wait 5 seconds and reexecute step 1.
4.if the message is not found after 10 retrys, stop the loop and append the log file: vs.log with "[date 10:31:27] Error Moving ps on Item 1000345, manual intervention is needed".
5.if message is found, exit script
Learning ...
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: script

Hi:

What have you written thus far?

I assume you know how to execute a command and redirect output to a file.

Using 'grep' is one way to read a file and look for a string or regular expression.

The 'sleep' command allows waiting.

Looping is easily done with a 'while condition which can be "complex".

A basic shell scripting guide and the 'sh-posix' manpages will guide you nicely.

Regards!

...JRF...
navin
Super Advisor

Re: script

I have an ouline ..but not sure how can i check an out put after greping an item from a log file and depending on that i have to perform one or the other task.
Please advice.
Thanks
Learning ...
James R. Ferguson
Acclaimed Contributor

Re: script

Hi (again):

> not sure how can i check an out put after greping an item from a log file and depending on that i have to perform one or the other task.

OK, look at the various options of 'grep' in the manpages. For example:

# grep local /etc/hosts
127.0.0.1 localhost loopback

# grep -c local /etc/hosts
1

# grep -q local /etc/hosts && echo "I matched" || echo "I didn't match"
I matched

# grep -q LOCAL /etc/hosts && echo "I matched" || echo "I didn't match"
I didn't match

In the first example, you could capture the output and test for emptiness or not:

RESULT=$(grep local /etc/hosts)
if [ -z "${RESULT}" ]; then
echo "nothing found"
else
echo "found something
fi

In the second example, you would test the count returned:

RESULT=$(grep -c local /etc/hosts)
if [ "${RESULT}" = 0 ]; then
echo "nothing found"
else
echo "found something
fi

In the third example, above, the return value from the 'grep' tells whether or not we had a match.

Regards!

...JRF...
navin
Super Advisor

Re: script

Hello ,
Thanks so much it really helped..i'm almost done...but would like to that once the condition is 0 i will have to reissue the command in 5 seconds ..after 10 retries i have to append an info in a log file and exit.Thanks for the assistance.
Learning ...
James R. Ferguson
Acclaimed Contributor

Re: script

Hi (again):

> but would like to that once the condition is 0 i will have to reissue the command in 5 seconds ..after 10 retries i have to append an info in a log file and exit.Thanks for the assistance

OK, here's an example:

#!/usr/bin/sh
typeset -i COUNT=0
typeset -i SEEN=0

while (( ${COUNT} < 10 ))
do
grep -qs "UPDATED" /var/tmp/mylog && { SEEN=1; echo "OK!"; break; }
let COUNT=${COUNT}+1
sleep 5
done
echo "SEEN=${SEEN}...going away"
exit 0

...Look at the manpages for 'sh-posix' and 'grep' for more information. If the 'grep' returns zero (0) meaning one or more matches, "OK" is printed, the variable SEEN is set to one (1) and the loop immediately exited.

Notice the use of the '-s' switch with 'grep' to suppress error messages for for nonexistent or unreadable files.

Regards!

...JRF...
navin
Super Advisor

Re: script

Thank You James.
Learning ...