0
#!/bin/bash
for x in ontwikkelkaart
do
    echo "***";
    echo ${x};

    ssh ${x}@localhost "
        find ~/public_html/wp-content/themes/ -type f -name "*.webp" | awk '{ gsub(".webp$", "") ; print $0 }' | xargs -i sh -c 'if [ ! -f "{}" ]; then echo {}.webp; fi' \;
    "
done

I have the above script that connects to a server via SSH, it checks wether there are webp files with no jpg/png as source file; and echo's rm "filename".

The command:

find ~/public_html/wp-content/themes/ -type f -name "*.webp" | awk '{ gsub(".webp$", "") ; print $0 }' | xargs -i sh -c 'if [ ! -f "{}" ]; then echo {}.webp; fi' \;

Works when i run it on the command line of the server (via SSH), but when i try to do it in the for loop, it does not work because of the "".

Can someone (try to) explain why the above code does not work?

2
  • 1
    better to do that is create an script and do ssh user@servername bash < your_local_script.bash Commented Feb 19, 2019 at 13:06
  • Or scp the file and run that on the target server with the ssh command. Commented Feb 19, 2019 at 14:02

1 Answer 1

1

Try using a heredoc :

ssh -q -T ${x}@localhost 2> /dev/null <<'EOF'
find ~/public_html/wp-content/themes/ -type f -name "*.webp" | awk '{ gsub(".webp$", "") ; print $0 }' | xargs -i sh -c 'if [ ! -f "{}" ]; then echo {}.webp; fi' \;
EOF
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but when i run the above i get the following error: images.sh: line 13: warning: here-document at line 7 delimited by end-of-file (wanted EOF') images.sh: line 14: syntax error: unexpected end of file`
@user2739456 Is the EOF line indented? It has to be at the beginning of the line
I'd suggest qutoing the first 'EOF' or variables such as $0 will get expanded inside the here-doc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.