Bash Script - umount a device, but don't fail if it's not mounted?
Asked Answered
D

4

23

I'm writing a bash script and I have errexit set, so that the script will die if any command doesn't return a 0 exit code, i.e. if any command doesn't complete successfully. This is to make sure that my bash script is robust.

I have to mount some filesystems, copy some files over, the umount it. I'm putting a umount /mnt/temp at the start so that it'll umount it before doing anything. However if it's not mounted then umount will fail and stop my script.

Is it possible to do a umount --dont-fail-if-not-mounted /mnt/temp? So that it will return 0 if the device isn't mounted? Like rm -f?

Downrange answered 4/9, 2009 at 10:5 Comment(0)
A
45

The standard trick to ignore the return code is to wrap the command in a boolean expression that always evaluates to success:

umount .... || /bin/true
Apology answered 4/9, 2009 at 10:9 Comment(5)
and redirect output to /dev/null: umount … > /dev/null 2>&1 || /bin/trueInexpressible
Well, that's sorta situational. It's easy to imagine a script wanting to continue, but leave any error messages in the log stream.Apology
You can make this a little faster by using the pseudo-command : (a shell builtin) instead of /bin/true (which creates a new process). That is, use "umount .... || :"Rayner
This assumes that the error reported by umount is because it was not mounted in the first place, but what about the case where the umount fails for other reasons? This could be dangerous!Diffluent
This is DANGEROUS -- like @LennartRolland said, you have no idea why umount failed here. The device may actually be busy and the umount may have failed Please don't use this!!Goddamn
G
15

Ignoring exit codes isn't really safe as it won't distinguish between something that is already unmounted and a failure to unmount a mounted resource.

I'd recommend testing that the path is mounted with mountpoint which returns 0 if and only if the given path is a mounted resource.

This script will exit with 0 if the given path was not mounted otherwise it give the exit code from umount.

#!/bin/sh

if mountpoint -q "$1"; then
  umount "$1"
fi

You an also do it as a one liner.

! mountpoint -q "$mymount" || umount "$mymount"
Georama answered 7/3, 2021 at 5:38 Comment(0)
R
7

Assuming that your umount returns 1 when device isn't mounted, you can do it like that:

umount … || [ $? -eq 1 ]

Then bash will assume no error if umount returns 0 or 1 (i.e. unmounts successfully or device isn't mounted) but will stop the script if any other code is returned (e.g. you have no permissions to unmount the device).

Radium answered 4/9, 2009 at 10:15 Comment(2)
I tested and umount quits with return 1 both for busy and not mounted so it is not possible to distinguish the two cases based on return code alone.Diffluent
Actually the return code is the number of mount points that failed to unmount, at least for util-linux's umount. Cf. github.com/karelzak/util-linux/blob/master/sys-utils/umount.cMaui
M
3

I just found the ":" more use ful and wanted a similar solution but let the script know whats happening.

umount ...... || { echo "umount failed but not to worry" ; : ; } 

This returns true with the message, though the umount failed.

Musk answered 26/6, 2012 at 11:57 Comment(2)
Most of the times it's more useful to use "/bin/true" for this purpose, it's then easier to read and see what it means.Percent
Aren't the {} and ; : ; redundant here? I tested if false || echo 1 ; then echo ok ; fi and the output was the same as if false || { echo 1 ; : ; } ; then echo ok ; fi.Benefaction

© 2022 - 2024 — McMap. All rights reserved.