Say I have a web application, with some sort of system execution. I’m going to use Ruby for this example. This app could be portable and installed on either a Windows or Unix server. If the app uses system commands, is there a way to distinguish what platform the server is, then maybe catch in an if statement?
Theoretically like this:
os_check = `checkos` # System ticks to execute through the shell
# using fake command
if os_check == 'unix'
# Run Unix Commands
`ls -la`
else if os_check == 'dos'
# Run DOS commands
`dir`
else
puts 'OS not detectable'
end
EDIT
I’m not looking for Ruby specifically (removed the tag). That was an example. I was hoping for a shell command that could execute in both environments and be variable based on what the OS is. I actually have to replicate this sort of function in several languages. Sorry for any confusion.
Try checking the
RUBY_PLATFORMglobal constant:[Edit] Per your updated question, you might be able to issue the system command
echo /?and parse the output to determine the host OS. The “echo” command should exist on most systems but Windows interprets the/?switch as the “help” message whereas UNIX OSes simply echo those characters. For example (again in Ruby, just to demonstrate the general strategy):