- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Script for loops, with multiple files
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
Forums
Discussions
Discussions
Discussions
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
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
02-04-2004 08:57 AM
02-04-2004 08:57 AM
This is what I have...
5 scripts, same name except it is script_1, script_2 etc.
Each of these scripts gives an output to a file... output1, output2, etc.
Instead of making variables for each file, I thought I can do a for statement...
I want to run each script, then check the output of each output and if it contains anything mail me.
I got the check for output part, thanks to S. Bhaskarla
But when I run what I have I get multiple entries and multiple emails!
I only want the 5 emails if there is data...
find $SCRIPTS_DIR/reset_pass* -print > /tmp/reset_pass.input
find $LOGS_DIR/login_entry_* -print > /tmp/login_entry.input
for i in `cat /tmp/reset_pass.input`
do
${i} #run the script
rm -r /tmp/host.not
for i in `cat /tmp/login_entry.input`
do
for HOST in $(awk '{print $1}' $SCRIPTS_DIR/sw_ip.txt |sort|uniq)
do
echo ${i}
grep ${HOST} ${i} |grep -q "changed passwd"
if [ ${?} = 1 ]
then
echo $HOST >>/tmp/host.not
mail -s "${i}, errors" $MAILTO < /tmp/host.not
fi
done
done
done
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2004 09:05 AM
02-04-2004 09:05 AM
SolutionIf you looked at my first response in the other post, I am checking for a variable called "STAMP" to see if it is set to 1. If it sets, then I will send the mail with the contents of the temporary file. I set STAMP to 1 when I find a case. This is your modified script to add that functionality.
find $SCRIPTS_DIR/reset_pass* -print > /tmp/reset_pass.input
find $LOGS_DIR/login_entry_* -print > /tmp/login_entry.input
for i in `cat /tmp/reset_pass.input`
do
${i} #run the script
rm -r /tmp/host.not
STAMP=0
for i in `cat /tmp/login_entry.input`
do
for HOST in $(awk '{print $1}' $SCRIPTS_DIR/sw_ip.txt |sort|uniq)
do
echo ${i}
grep ${HOST} ${i} |grep -q "changed passwd"
if [ ${?} = 1 ]
then
echo $HOST >>/tmp/host.not
STAMP=1
fi
done
if [ $STAMP = 1 ]
then
mail -s "${i}, errors" $MAILTO < /tmp/host.not
fi
done
done
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2004 09:07 AM
02-04-2004 09:07 AM
Re: Script for loops, with multiple files
for one, I would assign different loop variables for nested loops to avoid confusion. Then the return code of grep is 1 when no match was there. You have to compare to 0. Joint the two grep like grep -q "${HOST}.*changed passwd" ${i}
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2004 09:22 AM
02-04-2004 09:22 AM
Re: Script for loops, with multiple files
Michael's point is to be noted. 'i' is defined in first and second for loops. While it may work in this particular case, it is not a very good idea in general. It's better to use some meaningful names like 'for SCRIPT in `cat /tmp/reset_pass.input`, 'for ENTRY in `cat /tmp/login_entry.input` etc., Also, Posix and shell allow $() for command substitution so you don't have to worry about the ticks that get you confused sometimes with other quotes.
There is a small correction in my previous post. Put "STAMP=0" just above 'for HOST' loop.
for i in `cat /tmp/reset_pass.input`
do
${i} #run the script
rm -r /tmp/host.not
for i in `cat /tmp/login_entry.input`
do
STAMP=0
for HOST in $(awk '{print $1}' $SCRIPTS_DIR/sw_ip.txt |sort|uniq)
do
Rest remains the same.
It is a good idea to use {} while working with variables. But my personal preference is to use them only when needed. For ex., if I have to define a file name outputfile.${SCRIPT}.${DATE} to cause less confusion. However, I don't use {} convention necessarily for simple checks (if [ $? = 0 ])as it may be more confusing there.
Multiple greps can be combined into one.
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2004 10:57 AM
02-04-2004 10:57 AM
Re: Script for loops, with multiple files
All I should be getting is 5 emails, and only showing the IPs that do not have "change passwd"
Really appreciate another pair of eyes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2004 08:07 PM
02-04-2004 08:07 PM
Re: Script for loops, with multiple files
rm -r /tmp/host.not # delete the -r
No need to delete a single file recursively
sort|uniq can be shortened to sort -u
$? = 1 should be $? = 0 # see man grep for return codes
I really can't see, why you should get an endless loop. May be a lot of loops, but the script should terminate.
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2004 09:41 PM
02-04-2004 09:41 PM
Re: Script for loops, with multiple files
If that's not what you want, you need to check the creation of those files.
What you could do is add an echo to the mail line and then redirect the output of the script to a file and look at the size of the resulting files.
You could also skip all real actions and just print your loop variables to see if those are flowing correctly.
I see no usage of the outer loop variable in the inner loop variables, so the internal loop (with LOGIN_ENTRY) will result in the same output for all occurrences of the outer loop (with SCRIPT_EXP). Perhaps that's causing your problem?
And one more thing: you can send a mail for every LOGIN_ENTRY occurrence for every SCRIPT_EXP... Perhaps you should put that part after the 'done' line for the LOGIN_ENTRY loop, instead of before?
Because the removal of the host.not file is done before the 'for LOGIN_ENTRY' line, that seems what you want... Or you should put the removal of host.not after the 'for LOGIN_ENTRY' line.