“which in ruby”: Checking if program exists in $PATH from ruby

后端 未结 16 1074
萌比男神i
萌比男神i 2020-12-07 12:15

my scripts rely heavily on external programs and scripts. I need to be sure that a program I need to call exists. Manually, I\'d check this using \'which\' in the commandlin

16条回答
  •  無奈伤痛
    2020-12-07 12:41

    You can access system environment variables with the ENV hash:

    puts ENV['PATH']
    

    It will return the PATH on your system. So if you want to know if program nmap exists, you can do this:

    ENV['PATH'].split(':').each {|folder| puts File.exists?(folder+'/nmap')}
    

    This will print true if file was found or false otherwise.

提交回复
热议问题