Checking if a specific Unix/Linux command exists

I want to check if a command “port” is installed on Mac.

Here is what I have now:

File.open(“test.txt”, “w+”) do |f|
if system(“port > /dev/null 2>&1”)
f.puts %x(port installed)
else
f.puts “No MacPorts command was found.”
end
end

I tried to check if the command “port” exists and if the command is
found,
it executes the command with the argument. Otherwise,
it just prints the string “No MacPorts found”…

When running the script, it stalls. Possibly because
“port” command goes to interactive mode. I prevented
the output by using “/dev/null”, but it seems that the command
is still running, but not output is shown.

I was also playing with %x(). But I cannot find a solution.

Jon

Since you’re using the command line, why not use ‘which port’? it
should return the path to the port command (i.e., /use/local/bin/port).
You can always do a regex search for the success case of typing the port
command on a line by itself as well, if this seems like too
shell-dependent a solution.

Phil,

That “which” command should work fine. Thanks a lot!

Jon

2009/8/23 Hunt J. [email protected]:

Phil,

That “which” command should work fine. Thanks a lot!

In bash my preferred check is “type -a {command}” because it will give
you also functions and aliases.

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs