0

I want to run an awk command for a file in remote server. I connect with ssh and I run the following command:

cat /orch/servers | grep "+sNo+" | awk '{print $1}'

Type of this command's output is string. But I want to this output be array. The javascript code is below:

vmIDListCmd = "cat /orch/servers | grep "+sNo+" | awk '{print $1}'"
try {
session = new SSHSession(hostName, username, 22);
session.connectWithPasswordOrIdentity(passwordAuthentication, password);
vmIDList = session.executeCommand(vmIDListCmd, true);
} 
catch (e) {
throw "Unable to execute command: " + e;
} 
finally {
if (session) {
    session.disconnect();
}
System.log(vmIDList);
}

Output:

vm1 vm2 vm3 vm4

vmIDList variable (the output) is a string. I want to this variable be an array.

1 Answer 1

1

The string can be easily converted to an array using split.

var newArray = vmIDList.split(" ");
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this command. It's work but my array has just big 1 element? How can I fix this problem?
Check that in the string vm1 and vm2 are separated by a space (because it is the argument used in the split), and test with this example where I take the string, convert it to array and iterate it.
Sorry. It is an array but I use a tool called by "VMWare Orchestrator" for this project and there is a bug I think so. I must think another solution for split the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.