This question was migrated from Super User because it can be answered on Stack Overflow. Migrated两年前。Learn more
我想为远程服务器中的文件运行awk命令。我使用ssh连接并运行以下命令:
cat /orch/servers | grep "+sNo+" | awk '{print $1}'

此命令的输出类型为字符串。但我希望这个输出是数组。javascript代码如下:
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);
}

输出:
vm1 vm2 vm3 vm4

vmIDList变量(输出)是一个字符串。我希望这个变量是一个数组。

最佳答案

使用split可以很容易地将字符串转换为数组。

var newArray = vmIDList.split(" ");

08-25 00:50