最下面的代码已经发布成gem包,gem install eshell
github: https://github.com/xiaomi-sa/eshell
张汉东推荐了一个比较好玩的网站,http://explainshell.com
输入shell命令,可以将命令功能、参数说明等内容展现出来,比较适合linux新手学习使用。
github上有一个ruby实现的客户端,安装gem install explain_shell
不过它的功能只是根据输入的参数打开浏览器,访问explainshell.com这个网站,所以自己重新写了一个命令行下展示的工具。
用ruby重新写了一个查询展现工具,截屏如下:
使用方式
输入 ruby explain_shell.rb
会出现cmd>>,输入需要查询的命令即可
如:
1 2 3 4 5 6 7 8 |
true && { echo success; } || { echo failed; } cut -d ' ' -f 1 /var/log/apache2/access_logs | uniq -c | sort -n tar zcf - some-dir | ssh some-server "cd /; tar xvzf -" tar xzvf archive.tar.gz find . -type f -print0 ssh -i keyfile -f -N -L 1234:www.google.com:80 host iptables -A INPUT -i eth0 -s ip-to-block -j DROP git log --graph --abbrev-commit --pretty=oneline origin..mybranch |
改天有时间做成gem包发布,ruby代码如下:
explain_shell.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# encoding: utf-8 require 'nokogiri' require 'open-uri' require 'uri' require 'terminal-table' url = 'http://explainshell.com/explain?cmd=' def get_content(url) rows = [] index = {} cmd = [] color = [ "\e[0;96;49m", "\e[0;95;49m", "\e[0;94;49m", "\e[0;92;49m", "\e[0;91;49m", "\e[0;93;49m", "\e[0;36;49m", "\e[0;35;49m", "\e[0;34;49m", "\e[0;32;49m", "\e[0;31;49m", "\e[0;33;49m" ] begin doc = Nokogiri::HTML(open(url).read.strip) abort "\e[0;91;49mMissing man page, abort!\e[0m" if doc.content =~ /missing man page/ i = 0 doc.xpath('//span').each do |l| if l.get_attribute("helpref") =~ /^help*/ index[l.get_attribute("helpref")] = "#{color[i%12]}#{l.content}\e[0m" cmd << "#{color[i%12]}#{l.content}\e[0m" i += 1 end end doc.xpath('//pre').each do |l| if l.get_attribute('id') =~ /^(help*)/ next if l.content =~ /Pipelines/ rows << [index[l.get_attribute('id')]] rows << [l.content] rows << :separator end end rows.pop table = Terminal::Table.new :rows => rows puts "" puts cmd.join(" ") puts table rescue => err puts err end end #run print "cmd>> " input = URI::escape(gets.strip).gsub(/&/, "%26") get_content(url+input) |
转载请注明出处,非常感谢~!
3 Comments
Good
Hurrah! In the end I got a weblog from where
I can genuinely obtain useful facts regarding my study and knowledge.
我也用wxpython了个