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


        #4
        You've still got the umount twice, avoiding that I thought was a goal of "except it's doing the same thing twice". Perhaps
        Code:
        # Unmount backup folder if requested."
        if [ "$dismount" = true ] ; then
        
            if ! nodismount=$(umount $backup_folder 2>&1); then
                echo unmount failed
                # umount did not work
                msg="unmount failed: ${nodismount##*:}'"
            else
                echo unmount worked
                # umount worked
                msg="unmounted."
            fi
            su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily Backup:' 'Backup $msg'"
        fi
        
        ##Script complete.
        exit 0
        (Untested)
        Regards, John Little

        Comment


          #5
          Thanks again John! Other than one tiny typo your code works very well.

          Please Read Me

          Comment


            #6
            Originally posted by oshunluvr View Post
            Other than one tiny typo your code
            I'm assuming you mean in this snippet:
            Originally posted by jlittle View Post
            msg="unmount failed: ${nodismount##*:}'"
            Windows no longer obstructs my view.
            Using Kubuntu Linux since March 23, 2007.
            "It is a capital mistake to theorize before one has data." - Sherlock Holmes

            Comment


            • oshunluvr
              oshunluvr commented
              Editing a comment
              Yup/ One extra single quote, lol. Kate caught it right away!
          Working...
          X