简体   繁体   中英

Bash Script - Check mounted devices

What I'm trying to do - In the end, I want a script that checks for 3 devices, an SD card, Backup1 & Backup2. They've all been set to auto-mount at their respective mountpoints. The script should check for SD card first, if this fails, then a warning should be sent, and nothing more. If SD is okay, but only one backup is mounted, then ask for confirmation to go ahead with rsync to mounted backup. If all devices are mounted, then rsync from SD card to both backups.

Currently I'm just trying to get the device check nailed, using echo commands. Here's what I have (after multiple attempts) -

if ! mount | grep /media/card >/dev/null
then
    echo "ERROR: SD card not mounted, aborting"
else
    if ! mount | grep /media/backup >/dev/null
        then
            if ! mount | grep /media/backup2 >/dev/null
            then
                echo "ERROR: No backup devices"
            else
                echo "CAUTION: Backup_2 missing, Backup_1 OKAY"
            fi
    else
        if ! mount | grep /media/backup2 /dev/null
        then
            echo "CAUTION: Backup_1 missing, Backup_2 OKAY"
        else
            echo "SUCCESS: All devices OKAY"
        fi
    fi
fi

Which isn't working. I'm getting confused by all the nested 'if's and surely there's a simpler way? Perhaps something that checks each device independently, then returns values that equate to the various combinations (0=no devices, 1=sd only, 2=sd & backup1, 3=sd & backup 2, 4 = all okay) which then is read and decides next part of script to run? If not, where has this way gone wrong?

Any help is appreciated

Solved! Part of the problem was using mount | grep /media/backup > /dev/null mount | grep /media/backup > /dev/null as there's an issue with similar names, so /media/backup2 being mounted meant it was counting both devices as mounted.

Using grep -q /media/backup2\\ /proc/mounts works in a similar way, or at least produces the same outcome, just without the naming issue.

I've also changed the way the if statements work, it now checks for SD card, if that fails, then it aborts. It then checks for which devices are mounted with a fairly simple flow.

Here's the code:

GNU nano 2.2.6 File: backupSD2.sh Modified

#!/bin/bash

#Check for SD

if ! grep -q /media/card\  /proc/mounts
then
    echo "ERROR: NO SD CARD"
    exit 0
else
    echo "SD OKAY..."
fi

#Check for Backup Devices

if (( grep -q /media/backup\  /proc/mounts ) && ( grep -q /media/backup2\  /proc/mounts ))
then
    echo "Both Backups mounted"

elif grep -q /media/backup\  /proc/mounts
then
    echo "CAUTION: Backup_2 missing, Backup_1 OKAY"

elif grep -q /media/backup2\  /proc/mounts
then
    echo "CAUTION: Backup_1 missing, Backup_2 OKAY"
else
    echo "ERROR: NO BACKUP DEVICES"
    exit 0
fi

After the 'echo' commands is where I'll be putting the rsync commands. The final exit 0 is probably unnecessary, but I don't think it's doing any harm and reassures that it's not going to try any more commands.

Use mountpoint , which returns success ( 0 ) if the path is a mounted device, and 1 otherwise:

#!/bin/bash

check_mountpoint(){
if  mountpoint -q $1
  then
                printf "%s\n" "$1 is mounted"
                return 0
            else
                printf "%s\n" "$1 is not mounted"
                return 1
fi
}

check_mountpoint /media/sd 
if [ $? -gt 0 ]
  then
    printf "%s\n" "aborting"
    exit 0
fi
check_mountpoint /media/backup1
check_mountpoint /media/backup2

this is your code corrected, i hope it will help.

if mountpoint -q /media/sd
  then
    if mountpoint -q /media/backup
      then
        if mountpoint -q /media/backup2
          then
            echo "SUCCESS: All devices OKAY"
            # run your command here
          else
            echo "CAUTION: Backup_2 missing, Backup_1 OKAY, SD OKAY"
            # run your command here
        fi
      else
       if mountpoint -q /media/backup2
          then
            echo "CAUTION: Backup_1 missing, Backup_2 OKAY, SD OKAY"
            # run your command here
          else
            echo "SD OKAY , BACKUP Devices missing"
        fi
    fi
  else
    echo "SD is missing , Exit"
fi

Something like:

die() {
    echo "$0[$$] ERROR: $@" >&2
    exit 1
}

grep -q /media/card\ /proc/mounts || die "/media/card not mounted"
grep -q /media/backup2?\ /proc/mounts || die "No backup devices"
grep -q /media/backup\ /proc/mounts || echo "CAUTION: Backup_1 missing, Backup_2 OKAY"
grep -q /media/backup\ /proc/mounts &&
    grep -q /media/backup2\ /proc/mounts &&
    echo "SUCCESS: All devices OKAY"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM