Checking if a specific Unix/Linux command exists

Discussion in 'Ruby' started by Hunt Jon, Aug 23, 2009.

  1. Hunt Jon

    Hunt Jon Guest

    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
     
    Hunt Jon, Aug 23, 2009
    #1
    1. Advertising

  2. Hunt Jon

    Phil Romero Guest

    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
     
    Phil Romero, Aug 23, 2009
    #2
    1. Advertising

  3. Hunt Jon

    Hunt Jon Guest

    Phil,

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

    Jon
     
    Hunt Jon, Aug 23, 2009
    #3
  4. 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/
     
    Robert Klemme, Aug 24, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer
    Replies:
    0
    Views:
    375
    VB Programmer
    Oct 22, 2003
  2. Totan
    Replies:
    0
    Views:
    1,075
    Totan
    Apr 17, 2006
  3. Replies:
    4
    Views:
    1,765
    Terry Hancock
    Apr 23, 2005
  4. Kevin Wan
    Replies:
    0
    Views:
    414
    Kevin Wan
    Jan 16, 2007
  5. Ulf Meinhardt
    Replies:
    8
    Views:
    6,302
Loading...

Share This Page