2

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 
10
  • 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?CommentedSep 18, 2015 at 13:12
  • Also: 1) Instead of [ 'ssh josh you probably mean [ $(ssh josh…) ] same with nc 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.
    – Marco
    CommentedSep 18, 2015 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.CommentedSep 18, 2015 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 joshCommentedSep 18, 2015 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.CommentedSep 18, 2015 at 21:42

1 Answer 1

1

Instead of this:

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 

You probably meant this:

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 

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.

1
  • 3
    I've been away a long while and have now returned (from/to scripting & tinkering) to find an old question answered. By reading between the lines and interpreting what I was trying to accomplish and then fixing my code you make the process of understanding that much clearer. Thank you!CommentedJan 27, 2021 at 10:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.