作曲・指導・C言語・Linux

金沢音楽制作

金沢音楽制作では、楽曲・楽譜の制作と、作曲や写譜などレッスンを行っています。

google-drive-ocamlfuse Manager

google-drive-ocamlfuseを対話型で操作するスクリプトです。「Google Driveを使う」でも述べましたが、Linuxで無理してGoogle Driveを使う必要はありません。DropBoxが公式でLinux用のパッケージを配布しています。

環境:Arch Linux、google-drive-ocamlfuse

実行画面

処理を数字で選択する対話式のスクリプトです。$HOMEGoogleDrive(変更可能)ディレクトリを作成し、そこにマウントします。アンマウント時には、中にファイルがなければディレクトリが削除されます。

>>> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
>>> _/ Google-Drive-Ocamlfuse Manager _/
>>> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
>>>
>>> Please input 1 to 5 or q.
>>> ===========================
>>> 1) Mount GoogleDrive
>>> 2) Unmount GoogleDrive
>>> 3) Update GoogleDrive
>>> 4) Lazy Unmount GoogleDrive
>>> 5) Check Condition
>>> q) Quit
>>> Input number: 1
>>>
>>> Mounted.

$ ls ~
Desktop   Downloads    Music     Tempalates
Document  GoogleDrive  Pictures  Videos

GoogleDrive Ocamlfuse Managerのコード

長い割には大したことしていません。とはいえ、エラー処理をもう少し丁寧にしてもよかったかもしれません。

#!/bin/bash

# MacOS: umount $GD &> /dev/null 2&>&1 
# Linux: fusermount -u $GD &> /dev/null 2&>&1

### Line Option 
readonly SCRIPT_NAME=${0##*/}
readonly VERSION=1.1

fn_main() {
  while true; do
    fn_show_script_name

    echo ">>> Please input 1 to 5 or q."
    echo ">>> ==========================="

    fn_show_menu

    echo -n "&> Input number: "
    read input

    echo ">>>"

    case $input in
      1)
        fn_mnt
        continue
        ;;
      2)
        fn_unmnt
        continue
        ;;
      3)
        fn_update
        continue
        ;;
      4)
        fn_lazy
        continue
        ;;
      5)
        fn_cond
        continue
        ;;
      [Qq])
        fn_quit
        break
        ;;
      *)
        continue
        ;;
    esac
  done
}


### Option Print
fn_print_help() {
  cat << END

Usage: $SCRIPT_NAME [OPTION]
Manager for Google-Drive-Ocamlfue. 
This will hlep you mount your Google drive.

1) Mount GoogleDrive
   If ~/GoogleDrive does not exsit, Makes ~/GoogleDrive.
2) Unmount GoogleDrive
   After unmount then removes ~/GoogleDrive. In the case of not unmount then try again.
3) Update GoogleDrive 
   Mount again after unmount.
4) Lazy Unmount GoogleDrive 
   Attend! Data may not be saved. 
5) Check Condition
   Please use the result as a guide.
q) Quit
   Type q or Q then quit this script.

Options:
  --help     Display this help and exit.
  --version  Display version and exit.
  
END
}


### Option Version
fn_print_version() {
  cat << END
$SCRIPT_NAME version $VERSION
Infomation <https://hhkb.jp&>

END
}


#===================#
#   Option Select   #
#===================#
while [ $# -gt 0 ]; do
  case "$1" in
    --help)
      fn_print_help
      exit 0
      ;;
    --version)
      fn_print_version
      exit 0
      ;;
    --)
      shift
      break;;
  esac
done


#=================#
#   Script Name   #
#=================#
fn_show_script_name() {
  echo ""
  echo ">>> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/"
  echo ">>> _/ Google-Drive-Ocamlfuse Manager _/"
  echo ">>> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/"
  echo ">>>"
}


#=====================================#
#   Mount directory for GoogleDrive   #
#=====================================#
GD=~/googleDrive


#==============================#
#   Cennection to GoogleDrive  #
#==============================#
fn_fn_connect() {
  google-drive-ocamlfuse $GD &> /dev/null 2&>&1
  return 1
}


#=================================#
#   Check busy with GoogleDrive   #
#=================================#
fn_fuser() {
  fusermount -u $GD &> /dev/null 2&>&1
  if [ $? = 1 ]; then
    echo ">>> GoogleDrive is busy now."
    echo ">>> Please do it later."
    fn_anykey
    echo ">>>"
    return 2
  fi
  google-drive-ocamlfuse -cc &> /dev/null 2&>&1 # Clear Chash
}


#==============================#
#   Wait until press any key   #
#==============================#
fn_anykey() {
  read -sn 1 -p "&> Press any key to continue..."
  echo ""
}


#=======================#
#   Mount GoogleDrive   #
#=======================#
fn_mnt() {
  if [ -d $GD ]; then
    fn_connect
    if [ $? = 1 ]; then
      echo ">>> Already mounted."
      fn_anykey
      echo ">>>"
      echo ">>>"
      return
    fi
  else
    mkdir $GD &> /dev/null 2&>&1
    fn_connect
  fi
  echo ">>> Mounted."
  echo ""
  exit
}


#=========================#
#   Unmount GoogleDrive   #
#=========================#
fn_unmnt() {
  if [ -d $GD ]; then
    fn_fuser
    [ $? = 2 ] && return  
    rmdir $GD &> /dev/null 2&>&1
    echo ">>> Unmount."
  else
    echo ">>> No mounted."
    fn_anykey
    echo ">>>"
    return
  fi
  echo ""
  exit
}
 

#===================================#
#   Unmount and mount GoogleDrive   #
#===================================#
fn_update() {
  if [ -d $GD ]; then
    fuser
    [ $? = 2 ] && return
    fn_connect
    echo ">>> Updated."
    echo ""
    exit
  else
    echo ">>> Not mounted."
    fn_anykey
    echo ">>>"
  fi
}


#==============================#
#   Lazy unmount GoogleDrive   #
#==============================#
fn_lazy() {
  fusermount -zu $GD &> /dev/null 2&>&1
  if [ $? = 0 ]; then
    echo -n "&> Are you sure you want to lazy this drive? [Y/n] "
    while true; do
      read answer
      case $answer in
        ''[Yy]*)
          echo ">>> Lazy Unmount GoogleDrive."
          rmdir $GD &> /dev/null 2&>&1
          break
          ;;
        [Nn]*)
          break
          ;;
        *)
          echo -n "&> Please input. [Y/n] "
          ;;
      esac
    done
  else
  echo ">>> Not mounted."
  fi
  fn_anykey
  echo ">>>"
}


#=================================#
#   Check GoogleDrive condition   #
#=================================#
fn_cond() {
  if [ -d $GD ]; then
    if ls -d $GD/* &> /dev/null 2&>&1; then
      echo ">>> Found. $GD"
      echo ">>> And already mounted."
    else
      echo ">>> Found. $GD"
      echo ">>> But not mounted (no files)."
    fi
  else
    echo ">>> Not found. $GD"
  fi   
  fn_anykey
  echo ">>>"
}


#======================#
#   Quit this script   #
#======================#
fn_quit() {
  echo ">>> Bye. "
  echo ""
}


#======================#
#   Show select menu   #
#======================#
fn_show_menu() {
  MENU=("&> 1) Mount GoogleDrive" "&> 2) Unmount GoogleDrive" \
        "&> 3) Update GoogleDrive" "&> 4) Lazy Unmount GoogleDrive" \
        "&> 5) Check Condition" "&> q) Quit")
  for show in "${MENU[@]}"; do
    echo $show
  done
}


fn_main

# HASE Kazuaki <https://psipsina.jp>

更新情報

  • 作成日:2020-07-04
  • 更新日:2021-01-14
  • 更新日:2022-05-02