From: ptkwt@...2.teleport.com (Phil Tomson) Date: 2001-01-23T08:10:01+09:00 Subject: [ruby-talk:9739] Workaround for Windows backquote problem First a little background: If you're running Ruby on Windows the following will not work: version = `ver` #or: dircontents = `dir` #of course there are better ways to do this anyway You get some error about the command not being found. [BTW: This isn't an issue on Perl, anybody know why? seems like we could get the same behaviour in Ruby.] The problem has to do with certain commands being built into the Windows command shell (command.com on '95 & '93, cmd.exe on NT & Win2K). In order to get it to work you need to prefix with either command.com or cmd.exe depending on which kind of Windows you're running. So the following will work: version = `command.com /c ver` #on Win(95|98) version = `cmd.exe /c ver` #on Win(NT|2k) This is obviously annoying since you might have a mix of various Windows(95|98|NT|2K|ME) machines around that you need to run your script on. So I came up with the following workaround that's working for me and I post it here hoping that others will find it useful and improve on it: #backtick.rb require 'rbconfig' include Config #platform neutral backquotes - works on Win(95|98|NT|2K) and on Linux alias oldBackquote ` #`#just to make all the colorization workout def `(cmd) command_shell = "" puts "In redefined backquote!" if $DEBUG if CONFIG["arch"] =~ /win/ if ENV.has_key?("OS") && ENV["OS"] =~ /nt/i puts "It's either NT or 2K, use 'cmd.exe /c '" if $DEBUG #it's either NT or Win2K command_shell = "cmd.exe /c " else #it's Win(95|98) puts "It's Win(95|98), use 'command.com /c '" if $DEBUG command_shell = "command.com /c " end else puts "It's a 'nix, do nothing" if $DEBUG end cmd = command_shell + cmd oldBackquote(cmd) end #now test it: version = `ver` puts "version is:\n #{version}" #THE END It's based on the fact that there is an OS entry in the environment variables section on NT and 2K, so I check for it's existence and then check to see if it's defined to be NT (as it is in NT and 2K). I'm not sure if this will work under ME, I don't have any ME machines to try it on. Another improvement would be to make it so it doesn't have to check the OS env variable everytime backquotes are encountered - I suppose some kind of closure would allow us to keep a state variable or something similer. Any ideas? Phil