?
Solved

howto if exit echo 1 if not 0

Posted on 2010-11-29
5
Medium Priority
?
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Assisted Solution

by:favoretti
favoretti earned 664 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 668 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 20

Assisted Solution

by:simon3270
simon3270 earned 668 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

650 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