Synology - Eject USB drive from bash



#!/bin/sh


## Global variables
SCRIPTNAME=$(basename $0)
SCRIPTDIR=$(dirname $0)
# all drive names
DEVS=`/usr/syno/bin/synousbdisk -enum|grep -v "^Total"`


## Functions

# Find USB hard drive device
find () {
  USBHD=`/usr/syno/bin/synousbdisk -enum|grep -o "$1"` # -o  Show only the matching part of line
  if [ -z "$USBHD" ]; then
    echo "USB-drive '$1' is not mounted. $SCRIPTNAME -devs to see mounted drives."
    exit 1
  fi
}

# Unmount drive
unmount () {
  cp /tmp/usbtab /tmp/usbtab.old
  # grep non-matching lines and overwrite /tmp/usbtab
  grep -v "$1" /tmp/usbtab.old > /tmp/usbtab
  rm -f /tmp/usbtab.old
  sync
  /usr/syno/bin/synousbdisk -umount "$1"
  echo "You may now disconnect the USB drive '$1'."
}

# overwrite parameter
customName () {
  # if parameter is not empty
  if [ "$1" != "" ]; then
    case "$1" in
      # if parameter begins with a minus (function names) - return the value
      -* ) echo "$1";;
      # else
      * )
        PARAM="$1"
        DEVICES="$DEVS" # prevent overwriting "$DEVS"
        IFS=" "; set -- $DEVICES
        for i in $@; do
          # get info of every drive, grep after "Share Name"-line and the given parameter (name)
          NAME=`/usr/syno/bin/synousbdisk -info "$i" | grep "Share Name" | grep -i "$PARAM"`
          # if name parameter matches
          if [ "$NAME" != "" ]; then
            echo "$i"
            break 1
          fi
        done
        # if the name does not match to any drive name - return the value
        if [ "$NAME" == "" ]; then
          echo "$PARAM"
        fi
      ;;
    esac
  # return the empty value
  else
    echo "$1"
  fi
}


## Handle parameter

PARAM1=$(customName "$1")
PARAM2=$(customName "$2")

# no parameter
if [ -z "$PARAM1" ]; then
  echo "No USB-drive selected."

# show all device names
elif [ "$PARAM1" = '-devs' ]; then
  echo "$DEVS"

# show device info
elif [ "$PARAM1" = '-info' ]; then

  # no 2nd parameter
  if [ -z "$PARAM2" ]; then
    DEVICES="$DEVS" # prevent overwriting "$DEVS"
    IFS=" "; set -- $DEVICES
    for i in $@ ;do
        echo `$SCRIPTDIR/$SCRIPTNAME -info "$i"`
    done

  else
    find "$PARAM2"
    echo `/usr/syno/bin/synousbdisk -info "$PARAM2"`
  fi

# show help
elif [ "$PARAM1" = "-help" ]; then
  echo "USAGE: $SCRIPTNAME [ DEVNAME | -all | -devs | -info [DEVNAME] | -help ]" Questo articolo è stato d'aiuto? | No

Dettagli articolo

Codice articolo:
126
Categoria:
Data di inserimento:
10-Feb-2014 9:23am
Visualizzazioni:
3.705
Valutazione (Voti):
(120)

Articoli correlati