Conditional execution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional execution
# 1  
Old 06-15-2010
Conditional execution

Hi All,

I want to echo a message in case a system is reachable by ping or echo a different message in case it's not reachable.

Sample code i wrote is
Code:
ping localhost -n 2 | grep 'ttl' > ping_op; ls ping_op > /dev/null && drReachable=Alive; echo -e `date`: \\t "DR server is reachable" >> dr_check.log

i thought if the grep command finds ttl only then it will create a file but it does not seem like that.

How can i achieve conditional execution for the same

i thought i will o/p the command to a file and do a line count but it is fine for hpux but it doesn't work for solaris, which outputs a single line, not reachable or alive.

I'm new to this stuff. maybe there are better approaches Smilie

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 06-15-2010 at 03:04 AM.. Reason: code tags
# 2  
Old 06-15-2010
Quote:
Code:
ping localhost -n 2 | grep 'ttl' > ping_op; ls ping_op > /dev/null && drReachable=Alive; echo -e `date`: \\t "DR server is reachable" >> dr_check.log

You don't need to grep as the ping command returns an error status when target is unreachable.
Code:
ping localhost -n 2 && { drReachable=Alive; echo -e "$(date): \\t DR server is reachable" >> dr_check.log; }

Whithout brackets, the command after the last semicolon would run in any case.
This User Gave Thanks to frans For This Post:
# 3  
Old 06-15-2010
&& will only check if the last exit code was true/0 and checking if redirecting to a ls to a file was successful is not helpful.
Afaik you can't implement a "else" conditions by this.

Example:
Code:
VAR=somehost
ping -c3 $VAR > /dev/null 2>&1
if [ $? != 0 ]; then
     echo "`date` -- Host $VAR can't be reached!"
else
     echo "`date` -- Host "$VAR is reachable."
fi

If you write something like ping/network monitoring script (maybe run via cron?), I suggest you leave out the ok message since this would produce tons of useless output. In this case you do not need a else-condition and could do something like
Code:
#!/bin/bash

VAR=somehost
LOG=somelogfile

ping -c3 $VAR > /dev/null 2>&1 || echo `date` '-- Host $VAR cant be reached!' >> $LOG

exit 0

I also recommend in this case, using something like a lock-file that is being touched in the beginning and deleted in the end of the script to ensure that this script does not run multiple instances in parallel.
This User Gave Thanks to zaxxon For This Post:
# 4  
Old 06-15-2010
will update you both in some time.
Will check out both the options. Smilie
# 5  
Old 06-15-2010
MySQL

you can try this Smilie

Code:
PL=`ping -c 10 localhost|grep "% packet"|cut -d"%" -f1|awk '{print $6}'`
if [ $PL -gt 50 ]
 then
echo `date` "-> DR server is unreachable $PL percent package loss"
  fi
echo `date` "-> DR server is reachable $PL percent package loss"

This User Gave Thanks to ygemici For This Post:
# 6  
Old 06-18-2010
I was using cygwin so it came to $7
i do not have access to the actual solaris server so have to check it.
Thanks a lot for the reply Smilie

Quote:
Originally Posted by ygemici
you can try this Smilie

Code:
PL=`ping -c 10 localhost|grep "% packet"|cut -d"%" -f1|awk '{print $6}'`
if [ $PL -gt 50 ]
 then
echo `date` "-> DR server is unreachable $PL percent package loss"
  fi
echo `date` "-> DR server is reachable $PL percent package loss"



---------- Post updated at 08:52 AM ---------- Previous update was at 08:30 AM ----------

Thanks a lot for the input guys
used something similar to

Code:
#!/bin/bash

VAR=somehost
LOG=somelogfile

ping -c3 $VAR > /dev/null 2>&1 || echo `date` '-- Host $VAR cant be reached!' >> $LOG

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bashrc File - Conditional Command Execution?

Hello All, I was wondering if there is a way to execute a command in my ".bashrc" file based on how I logged into the PC? I was thinking maybe there is a way to check how the user (*myself) logged in, maybe somehow with the who command along with something else, but I'm not sure... I know I... (7 Replies)
Discussion started by: mrm5102
7 Replies

2. Shell Programming and Scripting

Conditional execution

Here's an interesting (to me, anyway) little puzzle. Background: I have a process that restores a number of large(ish) files from tape backup. As an individual file is being written, it is owned by root, then the ownership is changed when that file is complete. Since this process can take... (3 Replies)
Discussion started by: edstevens
3 Replies

3. Shell Programming and Scripting

Conditional Execution of a Script.

I have a unix shell script Test.sh more Test.sh echo "Calling dbquery1.sh...." ./dbquery1.sh echo "Calling dbquery2.sh...." ./dbquery2.sh more dbquery1.sh sqlplus -s user1/password1@DB_SID @/tmp/storedprocedures/Hello.rcp I run Test.sh However, I do not want dbquery2.sh to be... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. Shell Programming and Scripting

search and conditional execution

Hi, I have a list of files with different filenames like nam0001.txt,pan0001.txt etc coming in /data/inbox from various places. I needed a shell script which would:- 1)searches for a word from the filenames (coming in inbox directory) provided in input parameter. 2)if found, put in... (2 Replies)
Discussion started by: Saiesh
2 Replies

5. UNIX for Dummies Questions & Answers

Conditional execution of statements

Hi , I have a script which has multiple awk and sed commands like below. First one :- find /root/src/ -name "*csv" -size 0 -exec rm {} \; Second one: - ls *SRE*.txt > SRT_TRAN.dat rm *asr*.txt Third one :- find /root/src/ -name '*.csv' | while read FILENAME ; do awk... (2 Replies)
Discussion started by: shruthidwh
2 Replies

6. Shell Programming and Scripting

Help on shell script conditional execution when CPU Idle > 60%

I need a shell script that will monitor a few conditions and not execute until the these conditions are met. The problem I'm having is that I can not perform a database snapshot (backup) of a sybaseIQ database unless the CPU Status Idle % is above 60% or the snapshot (backup) fails. If... (2 Replies)
Discussion started by: pancona99
2 Replies

7. Shell Programming and Scripting

Expect Issue Serial Forground Execution vs Concurrent Background Execution

I have an expect script that interrogates several hundred unix servers for both access and directories therein using "ssh user@host ls -l /path". The combination of host/path are unique but the host may be interrogated multiple times if there are multiple paths to test. The expect script is run... (2 Replies)
Discussion started by: twk
2 Replies

8. Shell Programming and Scripting

unix script for conditional execution

Below is my shell script. I am trying to execute two different BTEQ scripts depending on the day of the week. So on a saturday I will execute a certain BTEQ script and on other weekdays I will run the other script. #!/bin/ksh dt=`date +"%a"` if then bteq > final_output <<- EOF .run... (3 Replies)
Discussion started by: Mihirjani
3 Replies

9. Shell Programming and Scripting

Conditional execution and parallel jobs

how can i process jobs parallel with conditions below. Script1.ksh Script2.ksh Script3.ksh Script4.ksh Script5.ksh Script6.ksh Script7.ksh Script8.ksh Script9.ksh Script10.ksh After successful completion of Script1.ksh I need to run Script7.ksh. After successful... (4 Replies)
Discussion started by: ford2020
4 Replies

10. UNIX for Dummies Questions & Answers

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (1 Reply)
Discussion started by: abhinavsinha
1 Replies
Login or Register to Ask a Question