wllp
Enthusiast
Enthusiast

Using Guest Script Manager Package

Hi All,

I have some issues while retrieving variables specified in a script, using the Guest Script Manager Package.

Example of my script:

Bash:

myVar = "Hello World"

echo $myVar

When I execute the "Run in Script VM Guest", I noticed that the $myVar is empty/null

Any idea how can I retrieve this variable in the script?

0 Kudos
5 Replies
iiliev
VMware Employee
VMware Employee

Hi,

The first issue is that there should be no space characters around assignment operator, so the first line of your script should be

myVar="Hello World"

Then, I think there are several issues with the workflow 'Run Script In Guest' when preparing Bash command line. Could you open the workflow for edit, locate the scriptable task labeled 'set for Linux', and change its content to the following

scriptProgramPath = "/bin/bash";

script = script.replace(/\r\n/g, " ; ");

script = script.replace(/\n/g, " ; ");

script = script.replace(/\'/g, "\'\"\'\"\'");

scriptArguments = " > " + scriptOutputFile + " 2>&1 -c '" + script + "'";

useCmdAnsi = false;

Compare it with the original content below:

scriptProgramPath = "/bin/bash";

script = script.replace("\r\n", " ; ");

script = script.replace("\\\"", "'\"'");

script = script.replace(/\"/g, "\\\\\\\"");

scriptArguments = "-c \"bash > " + scriptOutputFile + " 2>&1 -c \\\"" + script + "\\\"\"";

useCmdAnsi = false;

So the differences are:

  • The new version handles both \r\n and \n as end-of-line characters
  • The new version replaces end-of-line characters with command separator ';' globally so it should work for scripts with more than 2 lines
  • The new version doesn't launch additional bash instance
  • The new version has less double quotes - easier to escape and count Smiley Happy

Note I'm not Bash guru so it's possible that the new version has its own issues, but for simple scripts like yours it should work.

0 Kudos
wllp
Enthusiast
Enthusiast

Hi,

Thanks! It works fine now. However, I have issues getting it work in functions

function myFunction {

   myVar="This is my variable"

   echo $myVar

}

echo "Calling function"

myFunction

It is not returning any echo outputs. It seems like it is not executing the function.

Any ideas? Thanks!

0 Kudos
wllp
Enthusiast
Enthusiast

Hi,

I have figured out that the echo in the function will not be outputting any values. However, I still have issues that the workflow executed successfully with no errors.

However, when I login to the VM itself, the required changes from the workflow were not updated. Has this got to do with the "Run Script In Guest" issues again?

Thanks

0 Kudos
iiliev
VMware Employee
VMware Employee

Yes, this is expected.

The code shown in the previous post that replaces "\r\n" and "\n" with ";" actually makes each line of the script to be treated as a separate shell command. If you want to execute a multi-line script you should comment these two lines in order for the whole script to be treated as a single Bash command.

0 Kudos
redsand007
Enthusiast
Enthusiast

I am having a problem getting the Guest Script Manager to execute a simple bash script:

cp /tmp/gg /tmp/gg2

Is there a reason why this bash command will not run?  Is it the way that it's being interpreted when it is passed...I'm unsure.  I am able to run this basic bash script:

sudo useradd [userName]

So I know that everything is working...just seems something is weird with syntax when it runs.  Any ideas?

0 Kudos