Solved

Help with bash script (if/case statement)

Posted on 2009-07-15
10
884 Views
Last Modified: 2013-12-26
Hi,

I'm trying to get a shell script to work but getting nowhere fast. Am sure that someone better at bash scripts than me could do this in their sleep!

I have a drbd process that spits out the following string when it's working:

[root@node1 bin]# cat /proc/drbd
version: 8.0.13 (api:86/proto:86)
GIT-hash: ee3ad77563d2e87171a3da17cc002ddfd1677dbe build by buildsvn@c5-x8664-build, 2008-10-03 10:12:56
 0: cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate C r---
    ns:364186416 nr:0 dw:6410824 dr:357810242 al:5453 bm:22396 lo:0 pe:0 ua:0 ap:0
      resync: used:0/61 hits:23851932 misses:27468 starving:0 dirty:0 changed:27468
      act_log: used:0/257 hits:1597253 misses:302538 starving:1172 dirty:297066 changed:5453

What I've been doing is grep'ing out the most of that, like so:

[root@node1 bin]# cat /proc/drbd | grep Connected | cut -c 5-59
cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate

What I want to do is test for the string in a script, and if its present return 1 if not 0.

If anybody could offer some assistance i'd be very grateful!

Thanks
George
0
Comment
Question by:georgemason
  • 5
  • 4
10 Comments
 
LVL 5

Expert Comment

by:dvz-
ID: 24861977
what string are you trying to test for, exactly?  the one printed?

cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate

?  or a string within that?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24861985
searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate"

[ $(grep -c $searchstring /proc/drbd) -gt 0 ] && exit 1 || exit 0
 
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24861996
... of course you must use my command in a script to not exit your shell!
0
 
LVL 1

Author Comment

by:georgemason
ID: 24861998
Basically I just want to test whether the command "cat /proc/drbd" output contains the string "cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate".

If it does, I want the script to output "1", if not "0".
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24862026
... to only output 1 or 0 use

searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate"
[ $(grep -c $searchstring /proc/drbd) -gt 0 ] && echo 1 || echo 0
 
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:georgemason
ID: 24862042
@woolmilkporc

Thanks for that - seem to be an error in your script though? If I run it from the script I get the following:

[root@node1 bin]# ./cluster-state.sh
grep: st:Primary/Secondary: No such file or directory
grep: ds:UpToDate/UpToDate: No such file or directory
./cluster-state.sh: line 2: [: /proc/drbd:1: integer expression expected
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24862267
... right you are, sorry

searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate"
[ $(grep -c "$searchstring" /proc/drbd) -gt 0 ] && echo 1 || echo 0

Forgot the quotes ( " " ) !
 
0
 
LVL 1

Author Comment

by:georgemason
ID: 24862298
Excellent! Thanks very much.

Don't s'pose you could spare a sec to explain how that works? I understand that grep is counting the number of times the string appears in /proc/drbd - but what does -gt 0 do (and how does that interact with the echo 1 command)?

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 125 total points
ID: 24862514
OK,

as you said, "grep -c" counts the occurences of the string in the file.

The $( ... ) construct is the POSIX compliant (and easier to handle) equivalent of the backtick syntax ( ` ... `) which starts a subshell, executes the command in it, and returns the result, in the case of "grep -c" a numeric value.

This value is then compared against "0", with "-gt" meaning "algebraically greater than".
The manpage of "test" is usually very informative, please issue "man test" to learn all about comparisons and other expressions.

The [ ... ]  syntax is a short form of the "if test ... then ..." construct,
where "&&" means "execute the next statement if the expression in square brackets returns true",
|| means "execute if false"

Short, the command tests if the result of "grep -c ..." is greater zero, and if yes executes "echo1", if not executes "echo 0".

The quotes i forgot in my first post were needed because the searchstring contains spaces. grep took the "st:..." thing as the name of the file in which to search.

HTH

Cheers

wmp







0
 
LVL 1

Author Closing Comment

by:georgemason
ID: 31603861
Excellent - that's more than I could have hoped for. All I need to do now is work out why snmpd isn't updating when the cluster fails over. Thanks again!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

10 Experts available now in Live!

Get 1:1 Help Now