This is my first ever Bash script and I've done the best I could to find most of the answers on my own, but I have finally come to a road block.
This script seems to (mostly) work, but I am not receiving the files on my iMac's side of things.
The idea here is having a dedicated RPi torrent box with auto-file xfer to main computer followed by directory/file cleanup on the RPi system for space saving.
This script appears to handle any directory and/or file thrown at it, spaces and such do not affect its ability to initiate SCP.
I need a review of syntax by someone experienced in Bash scripting to find my error. Here is my script in its entirety. Any offers to increase efficiency would be greatly appreciated.
Updated with corrections used so far.
Narrowing issue down: select_target_directory
function Did I do this target_directory_selected=
part correct? Not sure this variable is being filled.
#!/bin/bash # variables declared directory_on_localhost="/mnt/32gb_pny_usbdrive/completed/*" directory_on_remote_host_primary="/Volumes/Drobo/zIncoming" directory_on_remote_host_secondary="/Users/josh/Desktop/zIncoming" target_directory_selected="" # functions defined # This function basically verifies the Drobo is mounted on the iMac. select_target_directory () { if [ 'ssh [email protected] test -d /Volumes/Drobo/zIncoming' ] then target_directory_selected="$directory_on_remote_host_primary" else target_directory_selected="$directory_on_remote_host_secondary" fi } # This function copies target <directories/files> to the target directory (via scp) # and then deletes them from the local machine to conserve valuable storage space. process_the_files () { for current_target in $directory_on_localhost do scp -r "$current_target" [email protected]:"$target_directory_selected" rm -rf "$current_target" done } # main logic begins # [Tests "$directory_host" for contents] && [iMac status (i.e. powered off or on)] # IF "$directory_host" is not empty AND iMac is powered on THEN functions are invoked # And Main Logic is completed and script ends, ELSE script ends. if [ "$(ls -A $directory_on_localhost)" ] && [ 'nc -z 10.0.1.2 22 > /dev/null' ] then select_target_directory process_the_files else exit fi # main logic ends
directory_on_remote_host_primary
but use$directory_remote_primary
that you did not define. Am I wrong?[ 'ssh josh
you probably mean[ $(ssh josh…) ]
same withnc
later 2) Quote your variables:[email protected]:"$target_dir"
3) You don't need bash here, a#!/bin/sh
will do just fine and is more portable.'ssh josh'
versus$(ssh josh
[ `command` ]
or[ $(command) ]
to get the return value ofcommand
? How would you handle the case whencommand
isecho -z aaa
? Simply usingcommand
is enough and sane. If you want to throw away the output, well, usecommand >/dev/null
.