mardi 6 juin 2017

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

Aucun commentaire:

Enregistrer un commentaire