Solved

howto if exit echo 1 if not 0

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a python script 5 96
Convert curl speed_dwonload to Mbps 29 116
shell script to send sms 2 139
bash while loop reading input from data section in script 7 69
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…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 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