Solved

howto if exit echo 1 if not 0

Posted on 2010-11-29
5
500 Views
Last Modified: 2012-05-10
Hi all,

How can I write bash script that checks if mysqld is running.
I use netstat –tulpn | grep 3306, if the port exists that means mysqld is running.
I need to say if 3306 exist then echo 1 if not exit echo 0

#!/bin/bash
# declare STRING variable
STRING= netstat -tulpn | grep 3306
0
Comment
Question by:rawandnet
5 Comments
 
LVL 6

Assisted Solution

by:favoretti
favoretti earned 166 total points
ID: 34236922
A powerful one-liner:

netstat -tulpn | grep 3306 | grep -v grep | wc -l

It will count number of lines generated by the command and will auto-echo 1 if line is present, 0 if there's no output.

grep -v grep insures grep itself won't generate a match.
0
 
LVL 14

Accepted Solution

by:
ddsh79 earned 167 total points
ID: 34236928
you can try this

str=`netstat -tulpn | grep 3306`
if [ "$str" -eq "" ]
then
      echo "output is blank"
else
      echo "not blank"
fi
0
 
LVL 48

Expert Comment

by:Tintin
ID: 34237310
Simply do
#!/bin/bash

netstat -tulpn | grep 3306 >/dev/null || exit 1

Open in new window

0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 167 total points
ID: 34238542
if you really want to echo 0 or 1 rather than set the return value, then

netstat -tulpn | grep -q 3306 ; echo $?

will echo 1 is the netstat output includes 3306, 1 if it doesn't.

One concern here is if the number you are looking for appears within a longer string - if, for example, the port 53306 is being used, that would appear as "success" in the above test.  A better test would be:

netstat -tulpn | grep -q ":3306 " ; echo $?
0
 

Author Closing Comment

by:rawandnet
ID: 34359613
thanks for your helps guys, I am gone have to start learning some scripting
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now