0

This is what happens.

  1. I manually and successfully mount my USB drive using mount /media/usb from outside a shell script which I wrote. Therefore, the drive is working and mounted normally.

  2. The very same mount command exists in this shell script. If I run this shell script while the USB drive is mounted from outside the script, the script exits without executing the rest of the script by saying:

    mount: /media/usb: /dev/sdc1 already mounted on /media/usb.
           dmesg(1) may have more information after failed mount system call.`
    
  3. If the drive is NOT mounted manually from outside the script like in bullet #1, the script does the mount and executes the rest of the commands inside the script without exiting the script.

So, the question is how to resume the script even though the drive was already mounted?

6
  • 2
    Have you got set -e or a first #! line that includes the -e flag? Commented Aug 9, 2024 at 7:09
  • Nope! I will see about that. So, do you mean that this set -e helps in skipping errors? Thanks for your time. Commented Aug 9, 2024 at 7:53
  • @superlinux No, -e causes the script to exit if an error occurs (although its definition of what's an error is weird). You almost certainly do not want -e mode set in your script. See BashFAQ#105: "Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected?" Commented Aug 9, 2024 at 8:12
  • 1
    This isn't normal behaviour so I would suggest there's something else going on. Can you post the shell script (in your question)? Or better still, a minimal version of it that illustrates the problem Commented Aug 9, 2024 at 8:48
  • 2
    In the question, please, where (a) you can format it properly so it's easily readable, (b) people can find it easily rather than having it buried in comments that might even get deleted Commented Aug 9, 2024 at 9:06

3 Answers 3

0

You have to run a check to see if is mounted already, then determine to mount based on the conditional.

In your case, it's /dev/sdc.

#!/bin/bash

# Function to check state of the USB drive
function is_usb_ready() {
    # Check if the USB drive exists
    if lsblk -d -o NAME | grep -q "^sdc$"; then
        # Check if the USB drive is not mounted
        if ! mount | grep -q "/dev/sdc"; then
            return 0  # Plugged in and not mounted
        fi
    fi
    return 1  # Not plugged in or already mounted
}

# Call function and perform the proper task based on its return value
if is_usb_ready; then
    echo "USB drive exists, but not mounted. Mounting drive."
    sudo mount /dev/sdc /your/mount_point/here
else
    echo "USB drive is not plugged in or already mounted."
fi

# Rest of your code

exit 0

The first grep will check if the drive's already plugged in, and the second grep will determine if it's mounted. If not, do so, otherwise proceed.


This is a way to do the check via destination instead of source. Since both don't cover each other's potential scenario's, I'll put both, and leave the decision up to the user.

#!/bin/bash

mountpoint="/your/mount_point/here"

function is_usb_ready() {
    # Check for filesystem at the mount point
    if mountpoint -q "$mountpoint"; then
        echo "The USB drive is already mounted at $mountpoint."
        return 1  # Already mounted
    else
        # If not mounted, check for device
        if lsblk -d -o NAME,TRAN | grep -q "sdc.*usb"; then
            return 0  # USB drive exists and is not mounted
        fi
    fi
    return 1  # USB drive is not plugged in
}

# Call function and perform the proper task based on its return value
if is_usb_ready; then
    echo "USB drive exists, but not mounted. Mounting drive."
    # Mount the USB drive
    sudo mount /dev/sdc "$mountpoint"
else
    echo "USB drive is not plugged in or already mounted."
fi

# Rest of your code

exit 0

It's waltinator for the win! A very simple solution.

#!/bin/bash

mountpoint="/your/mount_point/here"

if sudo grep -q "$mountpoint" /etc/mtab; then
    echo "The USB drive is already mounted at $mountpoint."
else
    echo "Mounting the USB drive."
    sudo mount /dev/sdc "$mountpoint"
fi

# Rest of your code

exit 0
4
  • Rather than depending on /dev/sdc (or any other fixed device name) you might want to check whether the filesystem on $mountpoint is different to the one on the parent of $mountpoint Commented Aug 9, 2024 at 13:11
  • 1
    @ChrisDavies Fair enough. It may be a bit of an overkill for the scenario, and not always foolproof. I'll do both. Commented Aug 9, 2024 at 13:36
  • You could use the mountpoint command to check first. E.g. [ mountpoint -q /your/mountpoint/here ] && sudo mount ..., or grep /your/mountpoint/here /etc/mtab. Read man mountpoint mtab. Commented Aug 9, 2024 at 17:43
  • @waltinator You are the winner on that one. Touche, my friend. Commented Aug 9, 2024 at 18:35
0

For the benefit of future readers who may be running a different OS, here is a more general solution that supports nested mount points:

mountpoint="/your/mount_point/here"
mountdevice="/dev/sdc"

if mount | grep -Fq " $mountpoint "; then
    printf "%s is already in use.\n' "$mountpoint"
else
    printf 'Mounting "%s" on "%s".\n' "$mountdevice" "$mountpoint"
    sudo mount "$mountdevice" "$mountpoint"
fi

# Rest of your code
0

findmnt can make detecting the mounted/not-mounted state easy, and ordinary shell || or && operators can decide whether to invoke the mount command:

#!/usr/bin/env bash

mount_point='/your/mount/point'

# pre-mount commands go here

# is the filesystem already mounted?  if not, mount it
findmnt -m -M "${mount_point}" || mount "${mount_point}"

# post-mount commands go here

Add redirection around findmnt if you don't want to see its messages. The -n option can get rid of the output's header line. The -m option tells the command to look in the /etc/mtab file for mounted filesystems, and the -M option gives the mount point's path to look for. When the path is mounted, the exit status is 0 (success), and non-zero otherwise (usually 1).

You can use more verbose if foo; then bar; else baz; fi clauses instead of the shortcut operators if you prefer.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.