Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with bash script (if/case statement)

Posted on 2009-07-15
10
Medium Priority
?
905 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month6 days, 2 hours left to enroll

772 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