-1

I'm looking for a shell command in unix that has a non zero output that is uniform across all unix platforms (for example will allways return 1 on a unix system) and also has no (or zero value) output in windows.

I tried (in unix):

sh echo 1

but this returns:

cannot execute binary file

using

echo 1

would not fit this criteria since it produces an output in windows as well as unix.

15
  • uname seems to work on a lot of systems. Example: # uname -> FreeBSD. On another computer: #uname -> Linux
    – Hennes
    Commented Nov 4, 2012 at 19:32
  • on windows xp: 'uname' is not recognized as an internal or external command, operable program or batch file. Commented Nov 4, 2012 at 20:02
  • and strangely enough, my mac returned "Darwin"... haha Commented Nov 4, 2012 at 20:03
  • It should. See en.wikipedia.org/wiki/Uname :)
    – Hennes
    Commented Nov 4, 2012 at 20:03
  • 3
    This seems to ask two different questions. Based on the title of this question. Do you want to test to see if what kind or type of system it is being ran on, or are you trying to get exit codes from commands on Windows like you do on Mac, BSD, Linux, Unix?
    – vgoff
    Commented Nov 4, 2012 at 20:47

2 Answers 2

3

The dir command exists in both Linux and Windows and may be suitable for this. The dir command on windows does set the exit code for scripting. It also has a plus, in that it is a non-destructive command.

On Linux, according to the man pages these error codes are available.

Exit status:
 0  if OK,
 1  if minor problems (e.g., cannot access subdirectory),
 2  if serious trouble (e.g., cannot access command-line argument).
2
  • Really thanks to Joey for helping me flush this out, as I really went on a tangent.
    – vgoff
    Commented Nov 4, 2012 at 22:33
  • 1
    I wonder if using the information verifying the system version would help. Write a C++ Program to do 'absolutely nothing but fail' if it is on Windows. And write a portion that will return otherwise.
    – vgoff
    Commented Nov 7, 2012 at 22:03
2

Test for the existence of the SYSTEMROOT or SYSTEMDRIVE environment variables to confirm you're on Windows. If you insist on a real belt-and-suspenders approach, parse the value to confirm you find a C: or similar drive letter.

4
  • but on Windows cmd you use %SYSTEMROOT% which doesn't work on Unix shells
    – phuclv
    Commented Jun 14, 2017 at 16:34
  • Use the env command to dump the environment variables. It exists on both Unix/Linux and Windows. Commented Jun 15, 2017 at 2:24
  • There's no env command in Windows. You use set instead. Moreover even if you manage somehow to get the environment variables list then there's no common way to check if a word exists in that list or not. In Windows you use find(str) whereas in Unix you use grep
    – phuclv
    Commented Jun 15, 2017 at 2:28
  • Apologies, you're correct. When I checked cmd.exe, I didn't realize I had my own utils in the search path. Indeed, one should use the set command; it exists in both cmd.exe and bash. Commented Jun 16, 2017 at 3:24

Not the answer you're looking for? Browse other questions tagged .