Alex Rivera | Logout

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

Asked 2009-09-04T10:05:19.110
26

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?

Edit
Report

1 Answer

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.

answered 2012-06-26T11:57:57.243

Your Answer