0

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 josh@10.0.1.2 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" josh@10.0.1.2:"$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
  • 1
    There are several inconsistencies in your variable names. You define directory_on_remote_host_primary but use $directory_remote_primary that you did not define. Am I wrong? – Renaud Pacalet Sep 18 '15 at 13:12
  • Also: 1) Instead of [ 'ssh josh you probably mean [ $(ssh josh…) ] same with nc later 2) Quote your variables: josh@10.0.1.2:"$target_dir" 3) You don't need bash here, a #!/bin/sh will do just fine and is more portable. – Marco Sep 18 '15 at 16:11
  • Renaud - I definitely missed that, sigh. I probably shouldn't rename variables as much as I did making this. So good catch and thank you. – Joshua Hall Sep 18 '15 at 20:35
  • Marco - I tried running this as #!/bin/sh but it wouldn't execute, so changed back for now. Please help me understand the difference between 'ssh josh' versus $(ssh josh – Joshua Hall Sep 18 '15 at 20:38
  • Why are you using either [ `command` ] or [ $(command) ] to get the return value of command? How would you handle the case when command is echo -z aaa? Simply using command is enough and sane. If you want to throw away the output, well, use command >/dev/null. – Arthur2e5 Sep 18 '15 at 21:42
1

Instead of this:

if [ 'ssh josh@10.0.1.2 test -d /Volumes/Drobo/zIncoming' ]
then
    target_directory_selected="$directory_on_remote_host_primary"
else
    target_directory_selected="$directory_on_remote_host_secondary"
fi

You probably meant this:

if ssh josh@10.0.1.2 test -d /Volumes/Drobo/zIncoming
then
    target_directory_selected="$directory_on_remote_host_primary"
else
    target_directory_selected="$directory_on_remote_host_secondary"
fi

Note that [ 'non-empty string' ] is always true. You probably wanted to use a condition on the ssh command, the way I rewrote it for you.

Similarly, later in the script you probably want to replace [ 'nc -z 10.0.1.2 22 > /dev/null' ] with nc -z 10.0.1.2 22 > /dev/null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.