Solved

howto if exit echo 1 if not 0

Posted on 2010-11-29
5
504 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
PowerShell Get-Help 10 84
Determine Who is Runnig my Bash Shell Script 4 91
AWK: Pythagoras bp script 18 41
Use awk or sed to manipulate Columns in a File 12 38
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…
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…
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…

730 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