can't get this code to work


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers can't get this code to work
# 8  
Old 12-15-2003
oh, then you should take the email part out of the loop.

#!/bin/sh

for a in `cat ipaddresses`
do
#write error to log for checking
/usr/sbin/ping $a|grep -ic alive 2> error.log
done

#the following determines the previous action with error or not.
#-s for checking error.log size, if no error, then it will be empty.
if [[ -s error.log ]];
then
mail jshwood <<!!!
done with error
!!!
else
mail jshwood <<@@@
all success
@@@
fi
# 9  
Old 12-15-2003
it says

would it help if I told you it was Solaris 8

Its just send one e-mail all the time "all succes"

I also had to take one set of [] out to get it to work does that have anything to do with it? the code now looks like


#!/bin/sh

for a in `cat ipaddresses`
do
#write error to log for checking
/usr/sbin/ping $a|grep -ic alive 2> error.log
done

#the following determines the previous action with error or not.
#-s for checking error.log size, if no error, then it will be empty.
if [ -s error.log ];
then
mail jshwood <<!!!
done with errors
!!!
else
mail jshwood <<@@@
all success
@@@
fi


PS thanks for the comments they are really helpfull for learning! the -s does that automaticly check the size of the file?
Regards
Jonathan
# 10  
Old 12-15-2003
Hammer & Screwdriver

About the [[ or [, because I always write korn shell rather than bourne shell. Smilie

Yes, -s is one of option of 'if'. You can type man if or man sh to know more.

I think the error.log may useful for you so I added these statement for you. If it is zero size that mean no error write to log. The log is used for your reference and more easy to write the script to determine different action.

Cheers!
# 11  
Old 12-15-2003
appreciate this

is there another way that you know of with out using grep cause I don't think it's a good way personally cause you are looking for one specific thing!
# 12  
Old 12-15-2003
Please keep messages about a specific problem together in the same thread. Also, use your man pages instead of just getting someone to give you answers.

You need to look at the man page for "sh" or "if". You don't have the correct syntax. Good examples of sh scripting might be found in your startup files (/etc/rc2.d on Solaris - S69inet would be one with if - then -else - fi statements)

if
/usr/sbin/ping $a|grpe -ic alive
break
else
mail "email address" <<!!!
message
!!!
fi

Last edited by RTM; 12-15-2003 at 09:46 AM..
# 13  
Old 12-15-2003
I tried my posted before and found a error. It is no output to log from the grep although it can't get any pattern. So the solution is:

You can do it more simply

#!/bin/sh

for a in `cat ipaddresses`
do
#write log or not just depend on you
#Actually no need to use grep, because ping also has its return code. you can type echo $? to see it after type ping anyip. 0 usually means ok and 1 means fail. You can also use it if [ $? -eq 0 ] to determine the action. Anyway, you should type 'man if' first.

if /usr/sbin/ping $a
then
# ":" means do nothing, you may type any command you like. If you only want to know the fail one then just follow it.
:
else
mail jshwood << !!!
$a not accessible!
!!!
fi
done


The mailbox will receive the message like:
xxx.xxx.xxx.xxx or hostname not accessible!

Cheers!
# 14  
Old 12-16-2003
Computer Thanks tikual

Ta for all your help this is the code and it works more amzingly!

#!/bin/sh

for a in `cat /export/home/jshwood/ip/ipaddresses`
do
/usr/sbin/ping $a
leave=$?

if [ $leave -eq 1 ]; then
exit
fi
done

mailx -s "IPadresses" jsherwood@phoenixmedical.co.uk <<!!!
IP addresses taken
!!!

Thanks though really apreciated it!
apparently you can use an awk command to do it aswell so I am gonna work that one out well hopefully!
Regards
Jonathan
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. IP Networking

Discussion at work, would a router work pluging a cable in wan1 and lan1?

hi all. and sorry for the random question, but this sparkled a raging flame-war at work and i want more points of view situation a router, with linux of some sort, dhcp client requesting for ip in wan1 (as usual with wan ports) dhcp server listening in lan1, and assigning ip (as usual... (9 Replies)
Discussion started by: broli
9 Replies

2. Shell Programming and Scripting

My code worked on a Mac, now it does not work in another computer

I guess Mac has default bash. Then I copy my code to another comp and run it...And it gives me an error like "bad substitution".... How I can change my code??? Never had before this kind of situation. Help please. if then n=$(sort /Users/Natalie/lastserial | tail -1) ... (6 Replies)
Discussion started by: Natalie
6 Replies

3. Programming

Breakpoints dont work in Codeblocks, code does not build

Hey so I've written a program and it crashes every time I run it. I tried placing breakpoints to see what was going but to my surprise they do not work! I've placed them anywhere and they do not stop the execution of the program. In fact if I add code and hit rebuild it doesn't even build that... (1 Reply)
Discussion started by: Cambria
1 Replies

4. Shell Programming and Scripting

My script work on Linux but not work in sunos.

My script work on Linux but not work in sun os. my script. logFiles="sentLog1.log sentLog2.log" intial_time="0 0" logLocation="/usr/local/tomcat/logs/" sleepTime=600 failMessage=":: $(tput bold)Log not update$(tput rmso) = " successMessage="OK" arr=($logFiles)... (7 Replies)
Discussion started by: ooilinlove
7 Replies

5. UNIX for Dummies Questions & Answers

why my code does not work?

Hi there can anyone help me why my code is saying Badly placed ()'s. #!/bin/csh if ($#argv != 1) then echo "One file is required" exit 1 else set lines=(`grep '404' $1 | cut -d" " -f5`) foreach line ($lines) printf ("Abnormal sites: %s\n", $line) endif thanks in advance! :) (7 Replies)
Discussion started by: FUTURE_EINSTEIN
7 Replies

6. IP Networking

NIC will not work, but it did work.

I have a client machine that was built and loaded with SCO UNIX 2.1.3, (yes it is old). The machine worked fine on the closed network that I tested on in my shop. I then had to change it to the network that it would be connected to. Below is the host file, router and subnet mask file that I usually... (0 Replies)
Discussion started by: NC user
0 Replies

7. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

8. Programming

why the code cant work?

I'am new in C programming in UNIX. the code is as following: --------------------------------- #include<stdio.h> #include<unistd.h> int main() { printf("pid=%d\n",getpid()); return 0; } ---------------------------------- when I compile it(gcc) it output errors: Undefined ... (4 Replies)
Discussion started by: Namely
4 Replies
Login or Register to Ask a Question