简体   繁体   English

Bash脚本-检查已安装的设备

[英]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. 我想做的是-最后,我需要一个脚本来检查3种设备,即SD卡,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. 该脚本应首先检查SD卡,如果失败,则应发送警告,仅此而已。 If SD is okay, but only one backup is mounted, then ask for confirmation to go ahead with rsync to mounted backup. 如果SD正常,但仅安装了一个备份,则要求确认以进行rsync到已安装的备份。 If all devices are mounted, then rsync from SD card to both backups. 如果所有设备都已安装,则从SD卡rsync到两个备份。

Currently I'm just trying to get the device check nailed, using echo commands. 目前,我只是想使用echo命令使设备检查固定。 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? 我对所有嵌套的“ if”感到困惑,当然还有更简单的方法吗? 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? 也许可以独立检查每个设备,然后返回等于各种组合的值(0 =无设备,仅1 = SD,2 = SD&Backup1,3 = SD&Backup 2,4 =一切正常),然后读取该值并决定要运行的脚本的下一部分? 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 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. mount | grep /media/backup > /dev/null因为存在名称相似的问题,因此/ media / backup2被挂载意味着它将两个设备都视为已挂载。

Using grep -q /media/backup2\\ /proc/mounts works in a similar way, or at least produces the same outcome, just without the naming issue. 使用grep -q /media/backup2\\ /proc/mounts工作方式相似,或者至少产生相同的结果,只是没有命名问题。

I've also changed the way the if statements work, it now checks for SD card, if that fails, then it aborts. 我还更改了if语句的工作方式,现在它检查SD卡,如果失败,则中止。 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 GNU nano 2.2.6文件:backupSD2.sh已修改

#!/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. 在'echo'命令之后,我将放置rsync命令。 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. 最后的exit 0可能是不必要的,但我认为它不会造成任何危害,并确保不会再尝试其他命令。

Use mountpoint , which returns success ( 0 ) if the path is a mounted device, and 1 otherwise: 使用mountpoint ,如果路径是已安装的设备,则返回成功( 0 ),否则返回1

#!/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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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