Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am new at scripting ... I can do very basic stuff, but now I need a hand.

I have a local filesystem that only will be mounted when I need to do a backup.

I'm starting with this.

#!/bin/bash
export MOUNT=/myfilesystem

if grep -qs $MOUNT /proc/mounts; then
  echo "It's mounted."
else
  echo "It's not mounted."; then
  mount $MOUNT;
fi

As I said, I'm very basic at scripting. I heard that you can check the status of the mount command by looking at the return codes.

RETURN CODES
       mount has the following return codes (the bits can be ORed):
       0      success
       1      incorrect invocation or permissions
       2      system error (out of memory, cannot fork, no more loop devices)
       4      internal mount bug
       8      user interrupt
       16     problems writing or locking /etc/mtab
       32     mount failure
       64     some mount succeeded

I don't know how to check that. Any guidance?

share|improve this question
1  
You have a funny dangling ; then in your script. –  Mat May 17 '12 at 16:18

2 Answers 2

up vote 5 down vote accepted

You can check the status code of mount, and most well written executables, with the shell special parameter ?.

From man bash:

? Expands to the exit status of the most recently executed foreground pipeline.

After you run the mount command, immediately executing echo $? will print the status code from the previous command.

# mount /dev/dvd1 /mnt
  mount: no medium found on /dev/sr0
# echo $?
  32

Not all executables have well defined status codes. At a minimum, it should exit with a success (0) or failure (1) code, but that's not always the case.

To expand on (and correct) your example script, I added a nested if construct for clarity. It's not the only way to test the status code and perform an action, but it's the easiest to read when learning.

#!/bin/bash
mount="/myfilesystem"

if grep -qs "$mount" /proc/mounts; then
  echo "It's mounted."
else
  echo "It's not mounted."
  mount "$mount"
  if [ $? -eq 0 ]; then
   echo "Mount success!"
  else
   echo "Something went wrong with the mount..."
  fi
fi

For more information on "Exit and Exit Status", you can refer to the Advanced Bash-Scripting Guide.

share|improve this answer
1  
There's no need to export $MOUNT. This script will also break if the filesystem has any character that would undergo wordsplitting (spaces, etc.). You should always quote your expansions. –  Chris Down May 17 '12 at 18:49
    
@ChrisDown You're right. I only deleted the obvious dangling ';then'. In the future, feel free to edit my answer to make it more correct. –  uther May 17 '12 at 19:30
    
Er... this will still break. You have still not quoted the expansions. On your advice, I'll do it now. –  Chris Down May 17 '12 at 20:14
    
I also wouldn't recommend linking to the infamous "Advanced" Bash scripting guide, it's full of errors and will teach people to write bugs, not scripts. BashGuide is a far better alternative. –  Chris Down May 17 '12 at 20:15
1  
@ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by. –  uther May 17 '12 at 21:25

Many Linux distros have the mountpoint command. It can explicitly used to check if a directory is a mountpoint. Simple as this:

#!/bin/bash    
if mountpoint -q "$1"; then
    echo "$1 is a mountpoint"
else
    echo "$1 is not a mountpoint"
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.