Announcement

Collapse
No announcement yet.

bash: Help clean up code and shorten output from error notification

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    bash: Help clean up code and shorten output from error notification

    In a bash script, I do mounting and unmounting of a folder. I have the script send messages to my desktop via notify-send. I wish to trim this output slightly and have my code robust enough to notify me of the condition.

    The relevant variables in the script:
    Code:
    backup_folder="/mnt/root_backup/"
    dismount="true"
    NOTIFYUSER="myuser"
    NOTIFYDISPLAY=":0.0"​
    In this instance, the backup folder is mounted and the current script requests it to unmount via this command:
    Code:
    # Unmount backup folder if requested."
    if [ "$dismount" = true ] ; then
    umount "$backup_folder"
    su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily Backup:' 'Backup unmounted.'"
    echo "Backup unmounted"
    fi

    The output in the notification looks like:
    Daily Backup:
    Backup unmounted.
    which is what I want when it works.

    Note that there is no error condition checking, so it reports "backup unmounted" regardless whether it actually worked or not.
    The last couple of days, I had left a console window open at the backup folder, so it did not actually unmount it The script redirects the errors to a log file, so I could see what had happened. I decide to "upgrade" my script to tell my directly when this occurs.

    The replacement code for the above code snippet:
    Code:
    # Unmount backup folder if requested."
    if [ "$dismount" = true ] ; then
      umount "$backup_folder"
    fi
    # Check to see if unmount worked
    if [[ -d "$backup_folder" ]]
      then
    # umount did not work
        nodismount=$((umount "$backup_folder" 2>&1) | grep -oP '(?<=umount: ).*')
        su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily Backup:' 'Backup unmount failed. $nodismount'"
      else
    # umount worked
        su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily Backup:' 'Backup unmounted.'"
    fi
    
    ##Script complete.
    exit 0​
    Output notification:
    Daily Backup:
    Backup unmount failed. /mnt/root_backup: target is busy.
    1. This does what I want except it's doing the same thing twice. It seems like I could shorten the code a bit by not having to separately create the message content via running the umount command twice.

    2. The notification is a bit long. I'd like to trim "Backup unmount failed. /mnt/root_backup: target is busy" to "Backup unmount failed: target is busy" but I can't seem to be able to figure out how to sed or grep my way there.

    Please Read Me

    #2
    1. Testing the "return code" from commands is IMO the natural way to check for errors; that is what if statements do:
    Code:
    if ! nodismount=$(umount $backup_folder 2>&1); then
        echo unmount failed
       ...
    fi
    2. Message manipulation with grep, sed, cut and the like are ... overly complicated IMO. bash, and posix shells generally, have done prefix and suffix removal since the 1990s:
    Code:
        echo Backup unmount failed: "${nodismount##*:}"
    Regards, John Little

    Comment


      #3
      Brilliant John, thanks!

      Sanity check: this is the full replacement code?

      Code:
      # Unmount backup folder if requested."
      if [ "$dismount" = true ] ; then
      umount "$backup_folder"
      
      # Check to see if unmount worked
        if ! nodismount=$(umount $backup_folder 2>&1); then
      echo unmount failed
      # umount did not work
            su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily Backup:' 'Backup unmount failed: ${nodismount##*:}'"
          else
      echo unmount worked
      # umount worked
            su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily Backup:' 'Backup unmounted.'"
        fi
      fi
      
      ##Script complete.
      exit 0​
      ​

      Please Read Me

      Comment

      Working...
      X