0

My simple bash script to fetch images of a Raspberry Pi that are older than one minute:

#!/bin/bash

time=`date +"%FT%H_%M_%S"`
imagedir="/root/pilapse/images/"
files=`ssh pi find /home/pi/weatherPi-images/ -type f -mmin +1`

echo -e "\033[1;32mFetching images from Raspberry Pi\033[1;00m"

for currFile in "${files[@]}"; do
   echo -e "rsync -a --remove-source-files --info=progress2 -e ssh pi:$currFile $imagedir\n"
done

echo -e "rsync [...]" is here just a placeholder for debugging.

Expected output:

rsync -a --remove-source-files --info=progress2 -e ssh pi:/home/pi/weatherPi-images/2016-02-01T13_22_20.jpg /root/pilapse/images/
rsync -a --remove-source-files --info=progress2 -e ssh pi:/home/pi/weatherPi-images/2016-02-01T13_14_07.jpg /root/pilapse/images/

Actual output:

rsync -a --remove-source-files --info=progress2 -e ssh 

horizon:/home/pi/weatherPi-images/2016-02-01T13_22_20.jpg
    /home/pi/weatherPi-images/2016-02-01T13_14_07.jpg
    /home/pi/weatherPi-images/2016-02-01T13_18_45.jpg
    /home/pi/weatherPi-images/2016-02-01T13_13_37.jpg /root/pilapse/images/

Somehow bash seems to expand the array here, but why? In my understanding in the foreach-loop above currFile contains only one path.

0

1 Answer 1

2

You didn't create files as an array. Array needs parentheses:

files=(`ssh pi find /home/pi/weatherPi-images/ -type f -mmin +1`)

Note it can break if filenames contain whitespace.

2
  • Do you know if it is possible to feed an array of file paths directly into rsync? The method above might not be the most performant / elegant way as every request is a complete new rsync connection. Which will use 100% CPU on the Pi, leaving almost no room for other applications running on the Pi. (Raspberry Pi B)
    – Flatron
    Feb 1, 2016 at 12:46
  • rsync should support file lists. Its documentation mentions SRC... which means one or more files.
    – choroba
    Feb 1, 2016 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .