[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 909
  • Last Modified:

Help with bash script (if/case statement)

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
georgemason
Asked:
georgemason
  • 5
  • 4
1 Solution
 
dvz-Commented:
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
 
woolmilkporcCommented:
searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate"

[ $(grep -c $searchstring /proc/drbd) -gt 0 ] && exit 1 || exit 0
 
0
 
woolmilkporcCommented:
... of course you must use my command in a script to not exit your shell!
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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".
0
 
woolmilkporcCommented:
... 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
 
georgemasonAuthor Commented:
@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
 
woolmilkporcCommented:
... 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
 
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)?

0
 
woolmilkporcCommented:
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
 
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!
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now