bashvariablessshcatvnc

How to store multiple commands in a bash variable (similar to cat otherscript.sh)


For work I'm needing to connect to test nodes and establish a vnc connection so you can see the desktop remotely. It's a manual process with a bunch of commands that need to be executed in order. Perfect for automation using a bash script. The problem is that some commands need to be executed on the remote node after an ssh connection is established.

Currently I've got it working like this, where startVNC is a seperate bash file which stores the commands that need to be executed on the remote node after an ssh connection is established.

cat startVNC | sed -e "s/\$scaling/$scaling/" -e "s/\$address/$address/" -e "s/\$display/$display/" | ssh -X maintain@$host

For my question the contents of startVNC don't really matter, just that multiple commands can be executed in order. It could be:

echo "hello"
sleep 1
echo "world"

While for personal use this solution is fine, I find it a bit of a bother that this needs to be done using two separate bash files. If I want to share this file (which I do) it'd be better if it was just one file. My question is, is it possible to mimic the output from cat in some way using a variable?


Solution

  • Well, you could do:

    a="echo 'hello'\nsleep 2\necho world\n"
    echo -e $a
    #  output-> echo 'hello'
    #  output-> sleep 2
    #  output-> echo world
    echo -e $a | bash
    #  output-> hello
    #  waiting 2 secs
    #  output-> world
    

    The -e in echo enables the interpretation of the \n.