Help with bash script (if/case statement)


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!

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate"

[ $(grep -c $searchstring /proc/drbd) -gt 0 ] && exit 1 || exit 0
... of course you must use my command in a script to not exit your shell!
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

georgemasonAuthor Commented:
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".
... 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
georgemasonAuthor Commented:

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]# ./
grep: st:Primary/Secondary: No such file or directory
grep: ds:UpToDate/UpToDate: No such file or directory
./ line 2: [: /proc/drbd:1: integer expression expected
... 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 ( " " ) !
georgemasonAuthor Commented:
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)?


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.





Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
georgemasonAuthor Commented:
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.