Checking if a specific Unix/Linux command exists

H

Hunt Jon

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
 
P

Phil Romero

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.
 
R

Robert Klemme

2009/8/23 Hunt Jon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top