Solved

howto if exit echo 1 if not 0

Posted on 2010-11-29
5
501 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:
Dushyant Sharma 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

13 Experts available now in Live!

Get 1:1 Help Now