Advertisement
v1ral_ITS

Dropbox shell for file transfers [ terminal ]

Jun 15th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.97 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # DropShell
  4. #
  5. # Copyright (C) 2013-2014 Andrea Fabrizi <andrea.fabrizi@gmail.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. #
  21.  
  22. #Looking for dropbox uploader
  23. if [ -f "./dropbox_uploader.sh" ]; then
  24.     DU="./dropbox_uploader.sh"
  25. else
  26.     DU=$(which dropbox_uploader.sh)
  27.     if [ $? -ne 0 ]; then
  28.         echo "Dropbox Uploader not found!"
  29.         exit 1
  30.     fi
  31. fi
  32.  
  33. #For MacOSX, install coreutils (which includes greadlink)
  34. # $brew install coreutils
  35. if [ "${OSTYPE:0:6}" == "darwin" -o "${OSTYPE:0:7}" == "freebsd" ]; then
  36.     READLINK="greadlink"
  37. else
  38.     READLINK="readlink"
  39. fi
  40.  
  41. SHELL_HISTORY=~/.dropshell_history
  42. DU_OPT="-q"
  43. BIN_DEPS="id $READLINK ls basename ls pwd cut"
  44. VERSION="0.2"
  45.  
  46. umask 077
  47.  
  48. #Dependencies check
  49. for i in $BIN_DEPS; do
  50.     which $i > /dev/null
  51.     if [ $? -ne 0 ]; then
  52.         echo -e "Error: Required program could not be found: $i"
  53.         exit 1
  54.     fi
  55. done
  56.  
  57. #Check DropBox Uploader
  58. if [ ! -f "$DU" ]; then
  59.     echo "Dropbox Uploader not found: $DU"
  60.     echo "Please change the 'DU' variable according to the Dropbox Uploader location."
  61.     exit 1
  62. else
  63.     DU=$($READLINK -m "$DU")
  64. fi
  65.  
  66. #Returns the current user
  67. function get_current_user
  68. {
  69.     id -nu
  70. }
  71.  
  72. function normalize_path
  73. {
  74.     $READLINK -m "$1"
  75. }
  76.  
  77. ################
  78. #### START  ####
  79. ################
  80.  
  81. echo -e "DropShell v$VERSION"
  82. echo -e "The Interactive Dropbox SHELL"
  83. echo -e "Andrea Fabrizi - andrea.fabrizi@gmail.com\n"
  84. echo -e "Type help for the list of the available commands.\n"
  85.  
  86. history -r "$SHELL_HISTORY"
  87. username=$(get_current_user)
  88.  
  89. #Initial Working Directory
  90. CWD="/"
  91.  
  92. function sh_ls
  93. {
  94.     local arg1=$1
  95.  
  96.     #Listing current dir
  97.     if [ -z "$arg1" ]; then
  98.         "$DU" $DU_OPT list "$CWD"
  99.  
  100.     #Listing $arg1
  101.     else
  102.  
  103.         #Relative or absolute path?
  104.         if [ ${arg1:0:1} == "/" ]; then
  105.             "$DU" $DU_OPT list "$(normalize_path "$arg1")"
  106.         else
  107.             "$DU" $DU_OPT list "$(normalize_path "$CWD/$arg1")"
  108.         fi
  109.  
  110.         #Checking for errors
  111.         if [ $? -ne 0 ]; then
  112.             echo -e "ls: cannot access '$arg1': No such file or directory"
  113.         fi
  114.     fi
  115. }
  116.  
  117. function sh_cd
  118. {
  119.     local arg1=$1
  120.  
  121.     OLD_CWD=$CWD
  122.  
  123.     if [ -z "$arg1" ]; then
  124.         CWD="/"
  125.     elif [ ${arg1:0:1} == "/" ]; then
  126.         CWD=$arg1
  127.     else
  128.         CWD=$(normalize_path "$OLD_CWD/$arg1/")
  129.     fi
  130.  
  131.     "$DU" $DU_OPT list "$CWD" > /dev/null
  132.  
  133.     #Checking for errors
  134.     if [ $? -ne 0 ]; then
  135.         echo -e "cd: $arg1: No such file or directory"
  136.         CWD=$OLD_CWD
  137.     fi
  138. }
  139.  
  140. function sh_get
  141. {
  142.     local arg1=$1
  143.     local arg2=$2
  144.  
  145.     if [ ! -z "$arg1" ]; then
  146.  
  147.         #Relative or absolute path?
  148.         if [ ${arg1:0:1} == "/" ]; then
  149.             "$DU" $DU_OPT download "$(normalize_path "$arg1")" "$arg2"
  150.         else
  151.             "$DU" $DU_OPT download "$(normalize_path "$CWD/$arg1")" "$arg2"
  152.         fi
  153.  
  154.         #Checking for errors
  155.         if [ $? -ne 0 ]; then
  156.             echo -e "get: Download error"
  157.         fi
  158.  
  159.     #args error
  160.     else
  161.         echo -e "get: missing operand"
  162.         echo -e "syntax: get <FILE/DIR> [LOCAL_FILE/DIR]"
  163.     fi
  164. }
  165.  
  166. function sh_put
  167. {
  168.     local arg1=$1
  169.     local arg2=$2
  170.  
  171.     if [ ! -z "$arg1" ]; then
  172.  
  173.         #Relative or absolute path?
  174.         if [ "${arg2:0:1}" == "/" ]; then
  175.             "$DU" $DU_OPT upload "$arg1" "$(normalize_path "$arg2")"
  176.         else
  177.             "$DU" $DU_OPT upload "$arg1" "$(normalize_path "$CWD/$arg2")"
  178.         fi
  179.  
  180.         #Checking for errors
  181.         if [ $? -ne 0 ]; then
  182.             echo -e "put: Upload error"
  183.         fi
  184.  
  185.     #args error
  186.     else
  187.         echo -e "put: missing operand"
  188.         echo -e "syntax: put <FILE/DIR> <REMOTE_FILE/DIR>"
  189.     fi
  190. }
  191.  
  192. function sh_rm
  193. {
  194.     local arg1=$1
  195.  
  196.     if [ ! -z "$arg1" ]; then
  197.  
  198.         #Relative or absolute path?
  199.         if [ ${arg1:0:1} == "/" ]; then
  200.             "$DU" $DU_OPT remove "$(normalize_path "$arg1")"
  201.         else
  202.             "$DU" $DU_OPT remove "$(normalize_path "$CWD/$arg1")"
  203.         fi
  204.  
  205.         #Checking for errors
  206.         if [ $? -ne 0 ]; then
  207.             echo -e "rm: cannot remove '$arg1'"
  208.         fi
  209.  
  210.     #args error
  211.     else
  212.         echo -e "rm: missing operand"
  213.         echo -e "syntax: rm <FILE/DIR>"
  214.     fi
  215. }
  216.  
  217. function sh_mkdir
  218. {
  219.     local arg1=$1
  220.  
  221.     if [ ! -z "$arg1" ]; then
  222.  
  223.         #Relative or absolute path?
  224.         if [ ${arg1:0:1} == "/" ]; then
  225.             "$DU" $DU_OPT mkdir "$(normalize_path "$arg1")"
  226.         else
  227.             "$DU" $DU_OPT mkdir "$(normalize_path "$CWD/$arg1")"
  228.         fi
  229.  
  230.         #Checking for errors
  231.         if [ $? -ne 0 ]; then
  232.             echo -e "mkdir: cannot create directory '$arg1'"
  233.         fi
  234.  
  235.     #args error
  236.     else
  237.         echo -e "mkdir: missing operand"
  238.         echo -e "syntax: mkdir <DIR_NAME>"
  239.     fi
  240. }
  241.  
  242. function sh_mv
  243. {
  244.     local arg1=$1
  245.     local arg2=$2
  246.  
  247.     if [ ! -z "$arg1" -a ! -z "$arg2" ]; then
  248.  
  249.         #SRC relative or absolute path?
  250.         if [ ${arg1:0:1} == "/" ]; then
  251.             SRC="$arg1"
  252.         else
  253.             SRC="$CWD/$arg1"
  254.         fi
  255.  
  256.         #DST relative or absolute path?
  257.         if [ ${arg2:0:1} == "/" ]; then
  258.             DST="$arg2"
  259.         else
  260.             DST="$CWD/$arg2"
  261.         fi
  262.  
  263.         "$DU" $DU_OPT move "$(normalize_path "$SRC")" "$(normalize_path "$DST")"
  264.  
  265.         #Checking for errors
  266.         if [ $? -ne 0 ]; then
  267.             echo -e "mv: cannot move '$arg1' to '$arg2'"
  268.         fi
  269.  
  270.     #args error
  271.     else
  272.         echo -e "mv: missing operand"
  273.         echo -e "syntax: mv <FILE/DIR> <DEST_FILE/DIR>"
  274.     fi
  275. }
  276.  
  277. function sh_cp
  278. {
  279.     local arg1=$1
  280.     local arg2=$2
  281.  
  282.     if [ ! -z "$arg1" -a ! -z "$arg2" ]; then
  283.  
  284.         #SRC relative or absolute path?
  285.         if [ ${arg1:0:1} == "/" ]; then
  286.             SRC="$arg1"
  287.         else
  288.             SRC="$CWD/$arg1"
  289.         fi
  290.  
  291.         #DST relative or absolute path?
  292.         if [ ${arg2:0:1} == "/" ]; then
  293.             DST="$arg2"
  294.         else
  295.             DST="$CWD/$arg2"
  296.         fi
  297.  
  298.         "$DU" $DU_OPT copy "$(normalize_path "$SRC")" "$(normalize_path "$DST")"
  299.  
  300.         #Checking for errors
  301.         if [ $? -ne 0 ]; then
  302.             echo -e "cp: cannot copy '$arg1' to '$arg2'"
  303.         fi
  304.  
  305.     #args error
  306.     else
  307.         echo -e "cp: missing operand"
  308.         echo -e "syntax: cp <FILE/DIR> <DEST_FILE/DIR>"
  309.     fi
  310. }
  311.  
  312. function sh_free
  313. {
  314.     "$DU" $DU_OPT info | grep "Free:" | cut -f 2
  315. }
  316.  
  317. function sh_cat
  318. {
  319.     local arg1=$1
  320.  
  321.     if [ ! -z "$arg1" ]; then
  322.  
  323.         tmp_cat="/tmp/sh_cat_$RANDOM"
  324.         sh_get "$arg1" "$tmp_cat"
  325.         cat "$tmp_cat"
  326.         rm -fr "$tmp_cat"
  327.  
  328.     #args error
  329.     else
  330.         echo -e "cat: missing operand"
  331.         echo -e "syntax: cat <FILE>"
  332.     fi
  333. }
  334.  
  335. while (true); do
  336.  
  337.     #Reading command from shell
  338.     read -e -p "$username@Dropbox:$CWD$ " input
  339.  
  340.     #Tokenizing command
  341.     eval tokens=($input)
  342.     cmd=${tokens[0]}
  343.     arg1=${tokens[1]}
  344.     arg2=${tokens[2]}
  345.  
  346.     #Saving command in the history file
  347.     history -s "$input"
  348.     history -w "$SHELL_HISTORY"
  349.  
  350.     case $cmd in
  351.  
  352.         ls)
  353.             sh_ls "$arg1"
  354.         ;;
  355.  
  356.         cd)
  357.             sh_cd "$arg1"
  358.         ;;
  359.  
  360.         pwd)
  361.             echo $CWD
  362.         ;;
  363.  
  364.         get)
  365.             sh_get "$arg1" "$arg2"
  366.         ;;
  367.  
  368.         put)
  369.             sh_put "$arg1" "$arg2"
  370.         ;;
  371.  
  372.         rm)
  373.             sh_rm "$arg1"
  374.         ;;
  375.  
  376.         mkdir)
  377.             sh_mkdir "$arg1"
  378.         ;;
  379.  
  380.         mv)
  381.             sh_mv "$arg1" "$arg2"
  382.         ;;
  383.  
  384.         cp)
  385.             sh_cp "$arg1" "$arg2"
  386.         ;;
  387.  
  388.         cat)
  389.             sh_cat "$arg1"
  390.         ;;
  391.  
  392.         free)
  393.             sh_free
  394.         ;;
  395.  
  396.         lls)
  397.             ls -l
  398.         ;;
  399.  
  400.         lpwd)
  401.             pwd
  402.         ;;
  403.  
  404.         lcd)
  405.             cd "$arg1"
  406.         ;;
  407.  
  408.         help)
  409.             echo -e "Supported commands: ls, cd, pwd, get, put, cat, rm, mkdir, mv, cp, free, lls, lpwd, lcd, help, exit\n"
  410.         ;;
  411.  
  412.         quit|exit)
  413.             exit 0
  414.         ;;
  415.  
  416.         *)
  417.             if [ ! -z "$cmd" ]; then
  418.                 echo -ne "Unknown command: $cmd\n"
  419.             fi
  420.         ;;
  421.     esac
  422. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement