├── COPYING ├── README.md ├── dbshi.sh └── dropbox_shell.sh /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Daniel Ray Jones 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | 3. The name of the author may not be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 20 | SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 22 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | dropbox-shell 2 | ============= 3 | 4 | A BASH script to run scripts/programs on a remote machine via Dropbox. 5 | 6 | First you have to set up a folder within your Dropbox with the following 7 | structure: 8 | Dropbox/remote/ 9 | Dropbox/remote/old/ 10 | Dropbox/remote/output/ 11 | Dropbox/remote/commands/ 12 | 13 | Optionally, the directory path can be specified as the initial argument, if a 14 | folder other than `Dropbox/remote` is desired. 15 | 16 | Previous versions allowed you to specify a separate directory in the form of 17 | `Dropbox/remote_{something}` with `{something}` as the initial argument. I have 18 | preserved this, however this behavior is deprecated, and may be removed in 19 | future versions. 20 | 21 | Place any executable files (scripts or compiled programs) into the commands 22 | folder. 23 | 24 | Then run the following command to execute scripts on your machine: 25 | `dropbox_shell.sh` 26 | or `dropbox_shell.sh {path}` 27 | 28 | All files in the commands folder will be executed. Output will be written to a 29 | log file in output, and the file will be moved to the old folder. 30 | 31 | Certain special files (Java .jar, etc.) may be placed in the folder, and they 32 | will be executed by their respective interpreters/virtual machines. 33 | 34 | Currently supported special files are: 35 | 36 | * `.class`: run as `java {classname}` 37 | * `.jar`: run as `java -jar {file}` 38 | * `.n`: run as `neko {file}` 39 | 40 | Additional special files can be supported by adding BASH functions to the 41 | script. The functions should be named like `run_${mimetype}_${extension}` where 42 | mimetype is the mime-type of the file (as determined by `file -bi $file`), with 43 | `/` being replaced with `_`, and all other non-alphabetic characters being 44 | removed. So, if the mime-type is `application/octet-stream`, and the file 45 | extension is `.foobar`, then the function definition would look something like 46 | this: 47 | 48 | function run_application_octetstream_foobar() 49 | { 50 | run_foobar "$1" 51 | } 52 | 53 | The file to be executed is passed as the sole argument to the function. 54 | 55 | Feel free to send me your additional functions, and I may add them to the 56 | program. 57 | 58 | If `file` is not available on the system, all files will be executed normally, 59 | so be sure to not use any special files on such a system. 60 | 61 | In addition to special files, special folders can be defined from which a 62 | user-defined function will be run on all files within the folder. By default, 63 | the script supports a `books` folder, and a `toprint` folder. If any file is 64 | placed in the `toprint` folder, it will be sent as a print job to the default 65 | printer. This works for pdf, ps, gif, jpg, bmp, and tif files. If any file is 66 | placed in the `books` folder, it will be added to your calibre library. To add 67 | your own special folders, first create the folder, then add to the folder name 68 | to `OTHERFOLDERS` array, then create a function called 69 | `run_folder_${foldername}` where foldername is the name of the folder. 70 | 71 | Other files will be made executable, and executed directly, so be sure to 72 | include a shebang in scripts. 73 | 74 | If no files are found in commands or any of the special folders, the program 75 | does nothing. 76 | 77 | Ideally, this should be added to a cronjob. My cronjob for this looks like this: 78 | `*/3 * * * * bash /path/to/dropbox_shell.sh` 79 | This runs it every three minutes. 80 | 81 | If a file in the commands folder is named "at-some time string", instead of 82 | executing it right away, it's assumed to be a bash script to be run using the 83 | "at" command, and "some time string" is the time to execute it. E.g., creating a 84 | script in the folder called "at-now + 20 minutes" will cause that file to be run 85 | twenty minutes from the time dropbox_shell is run. This form should only be used 86 | for shell scripts. 87 | -------------------------------------------------------------------------------- /dbshi.sh: -------------------------------------------------------------------------------- 1 | #!bin/bash 2 | 3 | #http://www.dropboxwiki.com/TipsAndTricks/RemoteControl2#Why_limit_yourself_to_individual_commands.3F_Get_a_shell.21 4 | 5 | -------------------------------------------------------------------------------- /dropbox_shell.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | REMOTEFOLD="${HOME}/Dropbox/remote" 4 | [ -n "$1" -a -d "$1" ] && REMOTEFOLD="$1" 5 | [ -n "$1" -a ! -d "$1" ] && REMOTEFOLD="${HOME}/Dropbox/remote_$1" 6 | COMFOLD="${REMOTEFOLD}/commands" 7 | OUTFOLD="${REMOTEFOLD}/output" 8 | OLDFOLD="${REMOTEFOLD}/old" 9 | PRINTFOLD="${REMOTEFOLD}/toprint" 10 | PRINTEDFOLD="${REMOTEFOLD}/printed" 11 | 12 | ## Processing special folders here. Check README for instructions 13 | OTHERFOLDERS=( books toprint ) 14 | function run_folder_books() { 15 | calibredb add "$1" 16 | } 17 | function run_folder_toprint() { 18 | ext="${1##*.}" 19 | case $ext in 20 | pdf) 21 | ps2pdf "$1" - | lp 22 | ;; 23 | ps) 24 | lp "$i" 25 | ;; 26 | jpg|gif|png|bmp|tif|tiff) 27 | convert "$1" pdf:- | pdf2ps - - | lp 28 | ;; 29 | *) 30 | echo "Can't print $ext file" 31 | ;; 32 | esac 33 | } 34 | 35 | ## Add functions to process special types. Check README for instructions 36 | function run_application_octetstream_n() { 37 | neko "$1" 38 | } 39 | function run_application_xjavaapplet_class() { 40 | java "${1%.class}" 41 | } 42 | function run_application_jar_jar() { 43 | java -jar "$1" 44 | } 45 | 46 | ## Default run function. Do not modify 47 | function run_default() { 48 | chmod +x "$1" 49 | "./${1}" 50 | } 51 | 52 | # If `file` command isn't present (msys), declare a dummy function 53 | # In this case, all commands will use the default run function 54 | if ! which file >/dev/null 55 | then 56 | function file() { 57 | echo "unknown/unknown" 58 | } 59 | fi 60 | 61 | if [ -d "$COMFOLD" ] 62 | then 63 | pushd "$COMFOLD" >/dev/null 64 | for i in * 65 | do 66 | if [ -f "$i" -a ! -e "$i.lock" -a "${i%%.lock}" == "$i" ] 67 | then 68 | mime="$(file -bi "$i")" 69 | mime="${mime%;*}" 70 | minmime=$(echo "$mime" | sed -e 's@/@_@g' -e 's/[^a-z_]//') 71 | ext="${i##*.}" 72 | 73 | if declare -f "run_${minmime}_${ext}" >/dev/null 74 | then dorun="run_${minmime}_${ext}" 75 | else dorun="run_default" 76 | fi 77 | 78 | touch "$i.lock" 79 | 80 | echo "==== Start: `date` ====" >> "${OUTFOLD}/${i}.log" 81 | if [ "${i#at-}" != "$i" ] 82 | then 83 | at -f "$i" "${i#at-}" >> "${OUTFOLD}/${i}.log" 2>&1 84 | else 85 | $dorun "$i" >> "${OUTFOLD}/${i}.log" 2>&1 86 | fi 87 | echo "===== End: `date` =====" >> "${OUTFOLD}/${i}.log" 88 | echo >> "${OUTFOLD}/${i}.log" 89 | mv --target-directory="${OLDFOLD}" "$i" 90 | rm "$i.lock" 91 | fi 92 | done 93 | popd >/dev/null 94 | fi 95 | 96 | for d in "${OTHERFOLDERS[@]}" 97 | do 98 | if [ -d "${REMOTEFOLD}/${d}" ] && declare -f "run_folder_${d}" >/dev/null 99 | then 100 | pushd "${REMOTEFOLD}/${d}" >/dev/null 101 | for i in * 102 | do 103 | if [ -f "$i" -a ! -e "$i.lock" -a "${i%%.lock}" == "$i" ] 104 | then 105 | touch "$i.lock" 106 | echo "=== Processing from $d: `date` ==" >> "${OUTFOLD}/${d}.log" 107 | echo "Processing $(basename "$i")" >> "${OUTFOLD}/${d}.log" 108 | run_folder_${d} "$i" >> "${OUTFOLD}/${d}.log" 2>&1 109 | echo "==== Processed from $d: `date` ==" >> "${OUTFOLD}/${d}.log" 110 | echo >> "${OUTFOLD}/${d}.log" 111 | mv --target-directory="${OLDFOLD}" "$i" 112 | rm "$i.lock" 113 | fi 114 | done 115 | popd >/dev/null 116 | fi 117 | done 118 | --------------------------------------------------------------------------------