How to save command result in variable in Jenkins shell build step
10,262
With single quotes the command works:
ssh yourserver 'echo "Start.";CYCLE=$(cat /your/file); echo $CYCLE; echo "End."'
ssh yourserver <<'EOF'
echo "Start."
CYCLE="$(cat /your/file)"
echo $CYCLE
echo "End."
EOF
Related videos on Youtube
![skymedium](https://i.stack.imgur.com/K63Gl.jpg?s=256&g=1)
Comments
-
skymedium almost 2 years
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?
-
skymedium almost 5 yearsThank you. It worked. I have chosen your 2nd approach.