0

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?

  • I just wonder why you want to do it. Your question feels to me as if it were a [XY Problem](https://en.wikipedia.org/wiki/XY_problem). – user1934428 Jun 01 '23 at 14:09
  • I want to automatically do some commands on startup since it saves me a lot of time. Otherwise I'd need to ssh into the device, set some system variables, startup x11vnc register the port and start the viewer. It's like 2~3 minutes max, but it's annoying and the time adds up. And I'd like it to be one file so it's more straightforward to share within the company – TomOnderwater Jun 02 '23 at 13:55
  • This I would understand. But do you need to have the whole commands in a variable? – user1934428 Jun 03 '23 at 14:04
  • Maybe not, but I needed something, and a variable seemed like a logical extension on what I already had working – TomOnderwater Jun 04 '23 at 21:39

1 Answers1

0

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.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31