- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Looping in a script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 04:13 AM
тАО05-19-2009 04:13 AM
#!/usr/bin/sh
function ftps {
sleep 20
USER=anonymous
PW=xxxxx
ftp -v -i -n
user $USER $PW
ascii
mget notification.log
bye
EOF
}
ftps
MAIL_RECEIVER="
a=`cat notification.log |head -n 5|sed 's/^.............................................//' > file.not.tmp`
b=`cat file.not.tmp |awk '{print $4}'`
if [[ $b = "
then
mailx -s "CDR backup is completed in blbilstg server" $MAIL_RECEIVER < file.not.tmp
fi
The scripts seems ok if the pattern is matched. But my concern is, I want to run the script until the pattern is not matched.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 04:25 AM
тАО05-19-2009 04:25 AM
Re: Looping in a script
If I'm understanding you correctly, you should be able to change the if statement at the end to
while [ "${b}" = "
do
...
done
I'd caution against this implemenetation, though. You're setting yourself up for problems if the file gets overwritten while it's being processed, if the ftp fails for any reason, etc.
HTH;
Doug O'Leary
------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 04:27 AM
тАО05-19-2009 04:27 AM
SolutionThere are several ways to do this. One way is to use a 'true' loop and 'break' out of it when the condition you want to see is satisfied:
#!/usr/bin/sh
function ftps {
sleep 20
USER=anonymous
PW=xxxxx
ftp -v -i -n server_name << EOF
user $USER $PW
ascii
mget notification.log
bye
EOF
}
ftps
MAIL_RECEIVER="
a=`cat notification.log |head -n 5|sed 's/^.............................................//' > file.not.tmp`
while true
do
b=`cat file.not.tmp |awk '{print $4}'`
if [[ $b = "
then
mailx -s "CDR backup is completed in blbilstg server" $MAIL_RECEIVER < file.not.tmp
break
fi
done
...Now, let's make some improvements to your general script. (1) Eliminate useless 'cat' processes! (2) the 'sed' match can be made much more readable with a repeat part; (3) eliminate the backticks and use the clearer POSIX shell $() notation for command execution:
#!/usr/bin/sh
function ftps {
sleep 20
USER=anonymous
PW=xxxxx
ftp -v -i -n server_name << EOF
user $USER $PW
ascii
mget notification.log
bye
EOF
}
ftps
MAIL_RECEIVER="
a=$(cat notification.log |head -n 5|sed 's/^.{45}//' > file.not.tmp)
while true
do
b=$(awk '{print $4}' file.not.tmp)
if [[ $b = "
then
mailx -s "CDR backup is completed in blbilstg server" $MAIL_RECEIVER < file.not.tmp
break
fi
done
Regards!
...JRF...
- Tags:
- evil cat
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 04:38 AM
тАО05-19-2009 04:38 AM
Re: Looping in a script
My concern is notification.log is overwritten and i do want to do ftp again until the pattern is matched.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 04:51 AM
тАО05-19-2009 04:51 AM
Re: Looping in a script
> My concern is notification.log is overwritten and i do want to do ftp again until the pattern is matched.
Since you are getting (with FTP) and then parsing this file, I would remove the file before performing the FTP. This also serves as a success-or-failure test:
...
rm -f notification.log
ftps
...
By the way, since you reference this file name multiple times, you should consider making it the value of a variable:
MYLOG=notification.log
...
rm -r ${MYLOG}
ftps
...
Too, I fail to see why you are using 'mget' instead of a simple 'get'. You aren't globbing for multiple files.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 05:02 AM
тАО05-19-2009 05:02 AM
Re: Looping in a script
BTW, where i can put this rm then ftps lines?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 05:14 AM
тАО05-19-2009 05:14 AM
Re: Looping in a script
> BTW, where i can put this rm then ftps lines?
As I showed, before the ftps() call.
#!/usr/bin/sh
set -u
typeset MYLOG=notification.log
typeset MYNOT=file.not.tmp
typeset MAILTO="you@your.net"
function ftps {
sleep 20
USER=anonymous
PW=xxxxx
ftp -v -i -n server_name << EOF
user ${USER} ${PW}
ascii
get ${MYLOG}
bye
EOF
}
rm -f ${MYLOG}
ftps
a=$(cat ${MYLOG}|head -n 5|sed 's/^.{45}//' > ${MYNOT})
while true
do
b=$(awk '{print $4}' ${MYNOT})
if [[ $b = "
then
mailx -s "CDR backup is completed in blbilstg server" ${MAILTO} < ${MYNOT}
break
fi
done
...
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 05:52 AM
тАО05-19-2009 05:52 AM
Re: Looping in a script
But i want if the pattern is not matched it again ftp the file and check accordingly until the matched found. If matched found than the program will terminate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 06:18 AM
тАО05-19-2009 06:18 AM
Re: Looping in a script
OK, your loop could look like this:
...
while true
do
rm -f ${MYLOG}
ftps
a=$(cat ${MYLOG}|head -n 5|sed 's/^.{45}//' > ${MYNOT})
b=$(awk '{print $4}' ${MYNOT})
if [[ $b = "
then
mailx -s "CDR backup is completed in blbilstg server" ${MAILTO} < ${MYNOT}
continue
else
break
fi
done
...
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2009 09:06 AM
тАО05-19-2009 09:06 AM
Re: Looping in a script
If you want a pattern instead of a simple string compare, you must use [[ ]]. And if you have a pattern, you must not use "".
>JRF: (1) Eliminate useless 'cat' processes!
Any reason you left this one? ;-)
a=$(cat ${MYLOG}|head -n 5|sed 's/^.{45}//' > ${MYNOT})
You could reverse the logic and save an indent for the main logic:
if [[ $b !=
break
fi
mailx -s "CDR backup is completed in blbilstg server" ${MAILTO} < ${MYNOT}