Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirm existance of executable (script, bat, cmd, exe) via a ruby file

Using a ruby file (or any rake facility) I need to find out if the user who executes my script is able to execute certain shell commands. In particular g++ etc. Hopefully system independent so if there is some g++.bat, g++.exe or just g++ (etc) it should say yes nevertheless, as long as its on the path and executable on the users system.

Example: if the user has a no-extention executable version of the file and a .cmd version of the file it should say "yes" for the no extension version on a linux system and "yes" to the .cmd version on a windows system. Since the users shell can only execute that version of the file.

The purpose of this is to allow the script to be self-configuring (as much as possible).

Any suggestions on how I might go about doing this?

like image 404
srcspider Avatar asked Dec 18 '25 18:12

srcspider


1 Answers

A quick and dirty way is to simply attempt to execute g++ via the system command and check the return code, for example:

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

You'd have to do some trickery to avoid getting unwanted output on the console (e.g. redirecting stdout/stderr based on correct OS syntax), but it couldn't be too bad.

like image 144
maerics Avatar answered Dec 20 '25 12:12

maerics



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!