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

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

  3. Hunt Jon

    Hunt Jon Guest

    Phil,

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

    Jon
     
    Hunt Jon, Aug 23, 2009
    #3
  4. In bash my preferred check is "type -a {command}" because it will give
    you also functions and aliases.

    Cheers

    robert
     
    Robert Klemme, Aug 24, 2009
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.