Javascript 使用shelljs将exec stdout与一些文本进行比较
运行Javascript 使用shelljs将exec stdout与一些文本进行比较,javascript,node.js,shell,shelljs,Javascript,Node.js,Shell,Shelljs,运行sh.exec('whoami')时,我试图比较标准输出,但它不起作用 我想做的是,在运行whoami命令后,如果结果变成一个特定的用户,那么我想触发一个操作 跑步的时候, sh.exec('whoami', (code, output) => { sh.echo(output == 'myusername'); // False console.log(output == 'myusername'); // False }); 条件output==“myusername”计
sh.exec('whoami')
时,我试图比较标准输出,但它不起作用
我想做的是,在运行whoami
命令后,如果结果变成一个特定的用户,那么我想触发一个操作
跑步的时候,
sh.exec('whoami', (code, output) => {
sh.echo(output == 'myusername'); // False
console.log(output == 'myusername'); // False
});
条件output==“myusername”
计算结果为false,尽管我完全确定它为true,因为我复制了用户名并将其作为比较字符串
预期结果是该条件的计算结果为true。Doing
console.log(sh.exec('whoami'));
您将获得以下输出
{ [String: 'username\n']
stdout: 'username\n',
stderr: '',
code: 0,
cat: [Function: bound ],
exec: [Function: bound ],
grep: [Function: bound ],
head: [Function: bound ],
sed: [Function: bound ],
sort: [Function: bound ],
tail: [Function: bound ],
to: [Function: bound ],
toEnd: [Function: bound ],
uniq: [Function: bound ]
}
问题只是一条新的线路stdout
结尾带有一个\n
。Doing
console.log(sh.exec('whoami'));
您将获得以下输出
{ [String: 'username\n']
stdout: 'username\n',
stderr: '',
code: 0,
cat: [Function: bound ],
exec: [Function: bound ],
grep: [Function: bound ],
head: [Function: bound ],
sed: [Function: bound ],
sort: [Function: bound ],
tail: [Function: bound ],
to: [Function: bound ],
toEnd: [Function: bound ],
uniq: [Function: bound ]
}
问题只是一条新的线路<代码>标准输出末尾带有一个
\n
。尝试记录输出
。那么它可能会有一些空间。还可以尝试通过char
对它们进行比较,查看它们是否使用相同的unicodewhoami
输出一个尾随换行符。可能会将“myusername\n”作为输出?非常感谢@AntihypeBird!我照你说的做了,注意到stdout在末尾加了一个\n。@gmotzespina没问题。很高兴您解决了这个问题,请尝试记录输出。那么它可能会有一些空间。还可以尝试通过char
对它们进行比较,查看它们是否使用相同的unicodewhoami
输出一个尾随换行符。可能会将“myusername\n”
作为输出?非常感谢@AntihypeBird!我照你说的做了,注意到stdout在末尾加了一个\n。@gmotzespina没问题。很高兴您解决了在回调函数的正文中传递给<代码> Exc< /C>的问题。在检查相等之前,请考虑使用该方法来删除<代码> \n>代码>。例如:sh.exec('whoami',(code,output)=>{sh.echo(output.trim()=='myusername');})代码>在回调函数的正文中传递给<代码> Exc< /Cord>。在检查相等之前,请考虑使用该方法来删除<代码> \n>代码>。例如:sh.exec('whoami',(code,output)=>{sh.echo(output.trim()=='myusername');})代码>