Confirm the existence of an executable file (script, bat, cmd, exe) through the ruby โ€‹โ€‹file

Using the ruby โ€‹โ€‹file (or any rake tool), I need to find out if the user running my script can execute some shell commands. In particular, g ++, etc. Hopefully system independent so if there is any g ++. Bat, g ++. Exe or just g ++ (etc.), it should say yes, however, as long as it is on the way and the executable is in the users system.

Example: if the user has an executable version of the file without the extension and the version of the .cmd file, he should say yes for the version without the extension on the linux system and yes for the version of .cmd on the Windows system. Since the user shell can only execute this version of the file.

The goal is to let the script configure itself (as much as possible).

Any suggestions on how I can do this?

+3
source share
3 answers

The quick and dirty way is to simply try to execute g ++ with a command systemand check the return code, for example:

def gpp_exists
  return system("g++ --version")
end

You will need to do some tricking to avoid unwanted console output (e.g. redirecting stdout / stderr based on the correct OS syntax), but this could not be too bad.

+4
source

, File exists?, executable?. ENV['PATH'] , ( , * nix - - Windows?). , .

Edit:

irb(main):001:0> ENV['PATH'].split(':').collect {|d| Dir.entries d if Dir.exists? d}.flatten.include? 'adduser'
=> true
irb(main):002:0> ENV['PATH'].split(':').collect {|d| Dir.entries d if Dir.exists? d}.flatten.include? 'foo'
=> false
+2

I need something like this (it runs on the way) and use the system command, but check the return code. according to this, this is the standard result in unix (127 = file not found) fooobar.com/questions/16791 / ...

    def checkinstalled(program)
      stdop=system(program)
      result=$?
      exit_code=result.exitstatus 
      return !exit_code.eql?(127)
    end

cannot indicate for windows however

+1
source

Source: https://habr.com/ru/post/1748430/


All Articles