Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash script – Output of find to here-string (<<<)

Line in my script trying to get the output of find to be in the here-string but I keep getting "put {}" with the obvious "{}: No such file or directory"

find "$SOURCE_DIR" -type f -name "*.txt" -exec sshpass -p "$PASSWORD" sftp -oPort=$PORT $USER@$HOST:$HOST_DIR <<< $'put' {} 2>&1 \;

How to I pass the filename into the here-string so that sftp will put the file?

My previous line in the script was this which I had no problems with. However, I can no longer use curl in this script.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

find "$SOURCE_DIR" -type f -name "*.txt" -exec curl -T {} sftp://$USER:$PASSWORD@$HOST:$PORT$HOST_DIR 2>&1 \;

>Solution :

find -exec doesn’t implicitly start a shell, so it doesn’t run shell operations or redirections.

You could make it start a shell (this is discussed in the Complex Actions section of Using Find), but it’s just as easy to write a NUL-delimited list of filenames, and read them into your shell:

while IFS= read -r -d '' file <&3; do
  sshpass -p "$PASSWORD" sftp -oPort="$PORT" "$USER@$HOST:$HOST_DIR" <<<"put $file"
done 3< <(find "$SOURCE_DIR" -type f -name "*.txt" -print0)

As an additional optimization, think about only running sftp once, not once per file (note that this is using the GNU -printf extension to find):

find "$SOURCE_DIR" -type f -name "*.txt" -printf 'put %p\n' |
  sshpass -p "$PASSWORD" sftp -oPort="$PORT" "$USER@$HOST:$HOST_DIR"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading