一括でコマンド（複数）のバージョンを表示

こんにちは。
一括でコマンド（複数）のバージョンを表示させるスクリプトです（MacOSX向け）。他の欲しい一覧も表示させるために処理が膨らんでしまいました。

```
$ ./checkv.rb
:
:
% /usr/bin/ruby -v
/usr/bin/ruby: Mach-O universal binary with 2 architectures
/usr/bin/ruby (for architecture x86_64):        Mach-O 64-bit executable x86_64
/usr/bin/ruby (for architecture i386):  Mach-O executable i386
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin13]
:
:
```

```
$ ./checkv.rb |grep "not\ found\|No\ such"
sh: R: command not found
sh: macgem: command not found
% mdls -name kMDItemVersion /Applications/Utilities/X11.app: No such file or directory
% cat /etc/redhat-release: No such file or directory
% lua: command not found
% sage: command not found
:
:
```

```checkv.rb
#!/usr/bin/ruby
require 'yaml'

$prompt = "% "
$commands_to_be_checked_yml = <<EOS
"":
    - "uname -a"
    - "sw_vers"
    - "hostinfo"
    - "mdimport -L"
    - "mdutil -sa |grep -B 1 disabled"
    - "qlmanage -m | sed -E 's/^.* -> \(.*\)$/\\\\1/' | grep -E ^/ |sort |uniq"
    - "ioreg -l -p IODeviceTree \| grep firmware"
    - "ifconfig en0 \|grep 'inet '"
    - "ifconfig en1 \|grep 'inet '"
    - "getconf LONG_BIT"
    - "file /mach_kernel"
    - "xcode-select -print-path"
    - "brew --config"
    - "brew list --versions \| awk '{printf(\\"%-18s %s\\\\n\\",$1,$2)}'"
    - "R CMD config --ldflags"
    - "echo 'cat(.Library)' | R --vanilla --slave "
    - "echo 'system(paste(\\"ls\\", .Library))' | R --vanilla --slave "
"ls":
    - ~/Library/QuickLook /Library/QuickLook /System/Library/QuickLook
    - ~/Library/QuickTime /Library/QuickTime /System/Library/QuickTime
"list":
    - gem
    - macgem
"plist":
    - /Library/Preferences/SystemConfiguration/com.apple.Boot.plist
"CFBundleShortVersionString":
    - /Applications/Utilities/XQuartz.app/Contents/Info.plist
    - /Applications/Xcode.app/Contents/version.plist
    - /Applications/Safari.app/Contents/version.plist
"kMDItemVersion":
    - /Applications/Utilities/X11.app
    - /Applications/Utilities/XQuartz.app
    - /Applications/Xcode.app
    - /Applications/Safari.app
"cat":
    - /etc/redhat-release
    - ~/.bash_profile
"echo":
    - CC
    - SHELL
    - PATH
    - MANPATH
    - ARCHFLAGS
    - SAGE_ROOT
    - PGDATA
    - GOPATH
"ps":
    - httpd
    - postgres
"-l":
    - apachectl
"-v":
    - lua
    - sage
    - perl
    - ruby
    - php
    - httpd
    - gem
    - macgem
    - cvs
    - unzip
    - gs
    - pdftotext
    - macruby
    - apachectl
"-V":
    - sudo
    - ssh
    - smbd
    - grep
    - /usr/bin/grep
    - nano
    - dot
    - less
    - lv
    - nkf
    - gzip
    - zcat
    - lzma
    - curl
    - wget
    - clamscan
    - clamdscan
    - freshclam
"-version":
    - emacs
    - sqlite3
    - X
    - /usr/X11R6/bin/X
    - Xquartz
    - lynx
    - scala
"--version":
    - bash
    - tcsh
    - zsh
    - gcc
    - llvm-gcc
    - clang
    - gcc-4.2
    - cc
    - CC
    - nvcc
    - gdb
    - lldb
    - gfortran
    - gfortran-4.2
    - vim
    - awk
    - sed
    - /usr/bin/sed # Darwin sed bundled in MacOSX
    - tar
    - /usr/bin/tar # bsdtar bundled in MacOSX
    - rails
    - brew
    - pip
    - python
    - ipython
    - pip3
    - python3
    - ipython3
    - /usr/bin/python # python bundled in MacOSX
    - R
    - r
    - svn
    - /Applications/Xcode.app/Contents/Developer/usr/bin/svn
    - git
    - /usr/bin/git
    - doxygen
    - m4
    - autoconf
    - automake
    - iconv
    - glibtool
    - platex
    - uplatex
    - xz
    - psql
    - julia
"version":
    - go
    - openssl
...
EOS

def run_cmd(cmd, opt="")
    return `#{cmd + " " + opt + " 2>&1"}`  # for sh/bash
end

def execute_cmd(cmd, checkfile=false)
    print $prompt + cmd
    cmdfile = cmd.split()[-1]
    if !checkfile || File.exist?(File.expand_path(cmdfile))
        puts "\n" + run_cmd(cmd)
    else
        print ": No such file or directory\n"
    end
end

def execute_plistbuddy(cmd, pentry="")
    pr = "'Print " + pentry + "'"
    execute_cmd("/usr/libexec/PlistBuddy -c #{pr} " + cmd, true)
end

def main(argv)
    YAML.load($commands_to_be_checked_yml).each {|opt, cmds|
        for cmd in cmds
            case opt
            when ''
                execute_cmd(cmd)
            when 'cat'
                execute_cmd("cat " + cmd, true)
            when 'echo'
                execute_cmd("echo $" + cmd)
            when 'list'
                execute_cmd(cmd + " list")
            when 'ps'
                execute_cmd("ps aux |grep " + cmd + " |grep -v grep")
            when 'ls'
                execute_cmd("ls " + cmd)
            when 'plist'
                execute_plistbuddy(cmd)
            when 'CFBundleShortVersionString'
                execute_plistbuddy(cmd, opt)
            when 'kMDItemVersion'
                execute_cmd("mdls -name kMDItemVersion " + cmd, true)
            else
                path = `#{"which " + cmd}`.chomp
                if path == '' 
                    print $prompt + cmd + ": command not found\n"
                else
                    print $prompt + path + " " + opt + "\n"
                    lsl = `#{"ls -l " + path}`
                    if lsl.include?(" -> ")
                        puts lsl.scan(/(\/.*)/)[0]
                    end
                    puts `#{"file " + path}`
                    puts run_cmd(cmd, opt)
                end
            end
            print "\n"
        end
    }
end

if $0 == __FILE__
  main(ARGV)
  exit(0)
end
```