Solved

Help with bash script (if/case statement)

Posted on 2009-07-15
10
890 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

821 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