2

I want to run this script inside my Jenkins job. My goal is to save the cat output into a variable so that I can use it somewhere else.

#!/bin/bash
ssh myserver.com  <<EOF
echo "Start."
CYCLE=$(cat /path/myfile.txt)
echo $CYCLE
...
echo "End."
exit
EOF

I get the following error in Jenkins log: cat: /path/myfile.txt: No such file or directory

I can run same command in terminal console successful. I think the command $(cat /path/myfile.txt) is executed before the connection to the remote server is up, because of https://stackoverflow.com/a/34074453/4565322. Also 'Start.' was printed after the error message although it should appear before the error message.

How Can I tell my script to wait with the execution of the cat command until the server connection is up?

1

With single quotes the command works:

ssh yourserver 'echo "Start.";CYCLE=$(cat /your/file); echo $CYCLE; echo "End."'

Also

ssh yourserver  <<'EOF'
echo "Start."
CYCLE="$(cat /your/file)"
echo $CYCLE
echo "End."
EOF
  • Thank you. It worked. I have chosen your 2nd approach. – skymedium Aug 27 at 8:42

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.