Guest User

Untitled

a guest
Mar 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # Command of the day
  3. # Picks a random executable from your path until it finds one with a manpage.
  4. # Use this to learn about some of the really obscure yet really useful scripts that are already on your system.
  5. DEBUG=ARGV.include?('--debug')
  6.  
  7. require 'timeout'
  8.  
  9. def debug(*args)
  10. puts args if DEBUG
  11. end
  12.  
  13. def has_man_page?(cotd_basename)
  14. debug " Looking for man-page for: #{cotd_basename}"
  15. man_path = Timeout::timeout(2) {`man -w #{cotd_basename} 2>&1`}
  16. if man_path.split("[\n\r]")[0].include?('No manual entry for')
  17. debug " No man-page found.",""
  18. return false
  19. end
  20. debug " man-page found at: #{man_path}",""
  21. return true
  22. rescue Timeout::Error
  23. debug " Timeout while looking for man-page for #{cotd_basename}"
  24. false
  25. end
  26.  
  27.  
  28. files = ENV["PATH"].split(":").uniq.compact.map { |path|
  29. Dir.glob(File.join(path,'*')).select {|file| File.file?(file) && File.executable?(file)}
  30. }.flatten.compact.uniq
  31. debug("#{files.size} commands found")
  32.  
  33. begin
  34. cotd = files.sample rescue files[rand(files.size)]
  35. cotd_basename = File.basename(cotd)
  36. debug("Random command picked: #{cotd_basename} (#{cotd})")
  37. end until has_man_page?(cotd_basename)
  38.  
  39. puts "Command of the day is: #{cotd}"
  40. if ARGV.include?('--man')
  41. exec "man #{cotd_basename}"
  42. else
  43. puts "Read more about it: `man #{cotd_basename}`"
  44. puts "Pass --man to automatically show the man-page."
  45. end
Add Comment
Please, Sign In to add comment