Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Checking if a specific Unix/Linux command exists

Reply
Thread Tools

Checking if a specific Unix/Linux command exists

 
 
Hunt Jon
Guest
Posts: n/a
 
      08-23-2009
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

 
Reply With Quote
 
 
 
 
Phil Romero
Guest
Posts: n/a
 
      08-23-2009

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.


On Sat, Aug 22, 2009 19:00, Hunt Jon wrote:
> 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


 
Reply With Quote
 
 
 
 
Hunt Jon
Guest
Posts: n/a
 
      08-23-2009
Phil,

That "which" command should work fine. Thanks a lot!

Jon

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-24-2009
2009/8/23 Hunt Jon <>:
> 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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if a directory exists? folder.exists() does not work! Ulf Meinhardt Java 8 08-28-2009 12:26 PM
finding if file exists or not in apache webdav server and if exists what is the mime type of it.... Totan Java 0 04-17-2006 05:13 AM
checking to see if DDL value exists Darrel ASP .Net 4 12-10-2004 03:23 PM
Checking to see if a cookie exists CES ASP .Net 2 02-13-2004 04:41 AM
HELP: Checking if directory exists off of web root VB Programmer ASP .Net 0 10-22-2003 02:03 PM



Advertisments