1

This question is built over a previous question I asked Determine OS from a single command line operation

I have a tool capable of executing a command on a remote system when I provide the host name. The remote machine can be Windows or Linux. After executing a command, I should be able to determine the OS from the result of the command.

The solution provided earlier was uname which prints "Linux" on linus OS and fails for windows. It works great theoretically but the problem I have is that any failure of command crashes the tool since Windows returns non zero return code in this case.

So to get around this issue I have two options in front of me

  1. Force windows command line to return 0 as the exit code irrespective of the actual result. (This has to be done as part of the single command I can execute on the windows system)
  2. Find a command (like echo) which will succeed on both systems (but unlike echo which gives the same result, should help me identify the OS)

I am not sure if the first option is possible, but a solution to either of them would help

Community
  • 1
  • 1
42cornflakes
  • 193
  • 4
  • 15

2 Answers2

4

whoami

Linux - will provide the user you are logged on as

Windows - will provide host/username

Noelkd
  • 7,686
  • 2
  • 29
  • 43
A-Bomb
  • 45
  • 3
3

I still think echo "$PATH" is the right idea here if you can base your decision on the output.

Since with a shell (bash, tcsh, ksh, etc.) on linux and cmd.exe or powershell on Windows you will get very different results.

With a linux/etc. shell you will get a path string.

With cmd.exe you will get "$PATH".

And with powershell you will get the empty string.

That empty string result is technically valid for linux/etc. since you can have an empty path there but you can work around that by using "${PATH:-linux}" instead which, for an empty (or unset) $PATH variable on in a sh-derived shell will get you linux and will still get you an empty string from powershell (though this fails for a csh derived shell it seems).

Community
  • 1
  • 1
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • I guess I was too impressed with the uname result that I overlooked this. Your solution works like a charm. +1 for the powershell inclusion too. – 42cornflakes Nov 10 '14 at 17:37