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."'

Also

ssh yourserver  <<'EOF'
echo "Start."
CYCLE="$(cat /your/file)"
echo $CYCLE
echo "End."
EOF
Share:
10,262

Related videos on Youtube

skymedium
Author by

skymedium

Android Coder.

Updated on September 18, 2022

Comments

  • skymedium
    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
    skymedium almost 5 years
    Thank you. It worked. I have chosen your 2nd approach.