1

When running sh.exec('whoami') I am trying to compare the stdout but it doesn't work.

What I want to do is that after running the whoami command if the results turns to be an specific user then I would like to trigger an action.

When running,

sh.exec('whoami', (code, output) => {
  sh.echo(output == 'myusername'); // False
  console.log(output == 'myusername'); // False
});

The condition output == 'myusername' evaluates to false even though I am completely sure it is true since I've copied the username and place it as the compared string.

The expected result would be that the condition evaluates to true.

Eddie
  • 26,593
  • 6
  • 36
  • 58
gmotzespina
  • 143
  • 7

1 Answers1

0

Doing,

 console.log(sh.exec('whoami'));

You get the following output,

{ [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 ] 
}

The problem was just a new line. stdout comes with a \n at the end.

gmotzespina
  • 143
  • 7
  • In the body of your callback function passed to `exec` consider utilizing the [`trim()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) method to remove the `\n` before checking for equality. For Instance: `sh.exec('whoami', (code, output) => { sh.echo(output.trim() === 'myusername'); });` – RobC May 02 '19 at 08:49