3

I have the following script to check if a NFS mount is currently mounted on the server :

#!/bin/bash
$targetserver=192.168.3.1
commandline="mount | grep '$targetserver' | wc -l"
checkmount=`$commandline`

if [ $checkmount == "1" ]; then
  echo "Mounted !"
else
  echo "Not mounted"
fi

But it seems that my checkmount is not returning anything.

What am I missing here ?

6
  • What do you get if you run the mount | grep ... bit on the command line? Commented Dec 17, 2010 at 12:18
  • returns '0' if not mounted, '1' if mounted. Just counting the lines Commented Dec 17, 2010 at 12:19
  • Just to be safe shouldn't you check >= 1 in case you have more folders mounted from the same NFS. Commented Dec 17, 2010 at 12:22
  • @Matt: good point, will check that. Commented Dec 17, 2010 at 12:27
  • @jgr has already put in a good answer, but I was going to suggest echoing the checkmount variable to make sure it's got the value you expect (i.e. 0 or 1). That's generally a good way to debug scripts. Commented Dec 17, 2010 at 12:29

2 Answers 2

12

This should work better.

#!/bin/bash
targetserver="192.168.3.1"
commandline=$(mount | grep "$targetserver" | wc -l)

if [ $commandline -gt 0 ]; then
  echo "Mounted !"
else
  echo "Not mounted"
fi

You could shorten it down though, using $?, redirection and control operators.

targetserver="192.168.3.1"
mount | grep "$targetserver" > /dev/null && echo "mounted" || echo "not mounted"

Depending on system grep /etc/mtab directly might be a good idea too. Not having to execute mount would be cleaner imho.

Cheers!

Sign up to request clarification or add additional context in comments.

4 Comments

Is there any reason to prefer $(...) syntax over backtick syntax? Just interested.
afaik backticks are more portable, i.e /bin/sh knows about them too.
@Cameron $() syntax is much easier to nest, and generally more readable. Backticks are slightly more portable, but almost no modern machine doesn't have a shell installed that accepts $() notation, which has been standardized for nearly 20 years. (/bin/sh on Solaris is the classic example, but /xpg4 contains plenty of modern shells) Recently, the autoconf maintainer (autoconf is notorious for portability concerns) has agreed that $() is acceptable. There is almost no reason for anyone to ever use backticks anymore.
I would have carried forward checkmount for the variable name. It's more meaningful in the context than "commandline".
3

I'd maybe do this, or just but the content of the function directly in if, if you just use it in one place.

nfsismounted() {
mount | grep -qm1 "$1":
}

q = quiet (we just want the return code), m1 = quit on first match.

And use it as such:

if nfsismounted 192.168.0.40; then
    echo "Mounts found"
else
    echo "Not mounts"
fi

A side note on the code in your question, you don't test with == in the shell, just =. == Will break on, for example, dash which is /bin/sh in Debian/Ubuntu since a while.

Edit: For added portability (non-GNU grep), remove the flags on grep and > /dev/null. Tests were done on bash/dash/ksh

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.