Merge pull request #3124 from dingdongnigetou/master

【翻译完毕】20150728 Understanding Shell Commands Easily Using 'Explain Shell' Script in Linux
This commit is contained in:
Xingyu.Wang 2015-08-04 20:26:30 +08:00
commit 5100aa5da3
2 changed files with 118 additions and 121 deletions

View File

@ -1,121 +0,0 @@
Translating by dingdongnigetou
Understanding Shell Commands Easily Using “Explain Shell” Script in Linux
================================================================================
While working on Linux platform all of us need help on shell commands, at some point of time. Although inbuilt help like man pages, whatis command is helpful, but man pages output are too lengthy and until and unless one has some experience with Linux, it is very difficult to get any help from massive man pages. The output of whatis command is rarely more than one line which is not sufficient for newbies.
![Explain Shell Commands in Linux Shell](http://www.tecmint.com/wp-content/uploads/2015/07/Explain-Shell-Commands-in-Linux-Shell.jpeg)
Explain Shell Commands in Linux Shell
There are third-party application like cheat, which we have covered here “[Commandline Cheat Sheet for Linux Users][1]. Although Cheat is an exceptionally good application which shows help on shell command even when computer is not connected to Internet, it shows help on predefined commands only.
There is a small piece of code written by Jackson which is able to explain shell commands within the bash shell very effectively and guess what the best part is you dont need to install any third party package. He named the file containing this piece of code as `'explain.sh'`.
#### Features of Explain Utility ####
- Easy Code Embedding.
- No third-party utility needed to be installed.
- Output just enough information in course of explanation.
- Requires internet connection to work.
- Pure command-line utility.
- Able to explain most of the shell commands in bash shell.
- No root Account involvement required.
**Prerequisite**
The only requirement is `'curl'` package. In most of the todays latest Linux distributions, curl package comes pre-installed, if not you can install it using package manager as shown below.
# apt-get install curl [On Debian systems]
# yum install curl [On CentOS systems]
### Installation of explain.sh Utility in Linux ###
We have to insert the below piece of code as it is in the `~/.bashrc` file. The code should be inserted for each user and each `.bashrc` file. It is suggested to insert the code to the users .bashrc file only and not in the .bashrc of root user.
Notice the first line of code that starts with hash `(#)` is optional and added just to differentiate rest of the codes of .bashrc.
# explain.sh marks the beginning of the codes, we are inserting in .bashrc file at the bottom of this file.
# explain.sh begins
explain () {
if [ "$#" -eq 0 ]; then
while read -p "Command: " cmd; do
curl -Gs "https://www.mankier.com/api/explain/?cols="$(tput cols) --data-urlencode "q=$cmd"
done
echo "Bye!"
elif [ "$#" -eq 1 ]; then
curl -Gs "https://www.mankier.com/api/explain/?cols="$(tput cols) --data-urlencode "q=$1"
else
echo "Usage"
echo "explain interactive mode."
echo "explain 'cmd -o | ...' one quoted command to explain it."
fi
}
### Working of explain.sh Utility ###
After inserting the code and saving it, you must logout of the current session and login back to make the changes taken into effect. Every thing is taken care of by the curl command which transfer the input command and flag that need explanation to the mankier server and then print just necessary information to the Linux command-line. Not to mention to use this utility you must be connected to internet always.
Lets test few examples of command which I dont know the meaning with explain.sh script.
**1. I forgot what du -h does. All I need to do is:**
$ explain 'du -h'
![Get Help on du Command](http://www.tecmint.com/wp-content/uploads/2015/07/Get-Help-on-du-Command.png)
Get Help on du Command
**2. If you forgot what tar -zxvf does, you may simply do:**
$ explain 'tar -zxvf'
![Tar Command Help](http://www.tecmint.com/wp-content/uploads/2015/07/Tar-Command-Help.png)
Tar Command Help
**3. One of my friend often confuse the use of whatis and whereis command, so I advised him.**
Go to Interactive Mode by simply typing explain command on the terminal.
$ explain
and then type the commands one after another to see what they do in one window, as:
Command: whatis
Command: whereis
![Whatis Whereis Commands Help](http://www.tecmint.com/wp-content/uploads/2015/07/Whatis-Whereis-Commands-Help.png)
Whatis Whereis Commands Help
To exit interactive mode he just need to do Ctrl + c.
**4. You can ask to explain more than one command chained by pipeline.**
$ explain 'ls -l | grep -i Desktop'
![Get Help on Multiple Commands](http://www.tecmint.com/wp-content/uploads/2015/07/Get-Help-on-Multiple-Commands.png)
Get Help on Multiple Commands
Similarly you can ask your shell to explain any shell command. All you need is a working Internet connection. The output is generated based upon the explanation needed from the server and hence the output result is not customizable.
For me this utility is really helpful and it has been honored being added to my .bashrc. Let me know what is your thought on this project? How it can useful for you? Is the explanation satisfactory?
Provide us with your valuable feedback in the comments below. Like and share us and help us get spread.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/explain-shell-commands-in-the-linux-shell/
作者:[Avishek Kumar][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/avishek/
[1]:http://www.tecmint.com/cheat-command-line-cheat-sheet-for-linux-users/

View File

@ -0,0 +1,118 @@
在Linux中利用"Explain Shell"脚本更容易地理解Shell命令
================================================================================
在某些时刻, 当我们在Linux平台上工作时我们所有人都需要shell命令的帮助信息。 尽管内置的帮助像man pages、whatis命令是有帮助的 但man pages的输出非常冗长 除非是个有linux经验的人不然从大量的man pages中获取帮助信息是非常困难的而whatis命令的输出很少超过一行 这对初学者来说是不够的。
![Explain Shell Commands in Linux Shell](http://www.tecmint.com/wp-content/uploads/2015/07/Explain-Shell-Commands-in-Linux-Shell.jpeg)
在Linux Shell中解释Shell命令
有一些第三方应用程序, 像我们在[Commandline Cheat Sheet for Linux Users][1]提及过的'cheat'命令。Cheat是个杰出的应用程序即使计算机没有联网也能提供shell命令的帮助 但是它仅限于预先定义好的命令。
Jackson写了一小段代码它能非常有效地在bash shell里面解释shell命令可能最美之处就是你不需要安装第三方包了。他把包含这段代码的的文件命名为”explain.sh“。
#### Explain工具的特性 ####
- 易嵌入代码。
- 不需要安装第三方工具。
- 在解释过程中输出恰到好处的信息。
- 需要网络连接才能工作。
- 纯命令行工具。
- 可以解释bash shell里面的大部分shell命令。
- 无需root账户参与。
**先决条件**
唯一的条件就是'curl'包了。 在如今大多数Linux发行版里面已经预安装了culr包 如果没有你可以按照下面的命令来安装。
# apt-get install curl [On Debian systems]
# yum install curl [On CentOS systems]
### 在Linux上安装explain.sh工具 ###
我们要将下面这段代码插入'~/.bashrc'文件LCTT注: 若没有该文件可以自己新建一个)中。我们必须为每个用户以及对应的'.bashrc'文件插入这段代码笔者建议你不要加在root用户下。
我们注意到.bashrc文件的第一行代码以#)开始, 这个是可选的并且只是为了区分余下的代码。
# explain.sh 标记代码的开始, 我们将代码插入.bashrc文件的底部。
# explain.sh begins
explain () {
if [ "$#" -eq 0 ]; then
while read -p "Command: " cmd; do
curl -Gs "https://www.mankier.com/api/explain/?cols="$(tput cols) --data-urlencode "q=$cmd"
done
echo "Bye!"
elif [ "$#" -eq 1 ]; then
curl -Gs "https://www.mankier.com/api/explain/?cols="$(tput cols) --data-urlencode "q=$1"
else
echo "Usage"
echo "explain interactive mode."
echo "explain 'cmd -o | ...' one quoted command to explain it."
fi
}
### explain.sh工具的使用 ###
在插入代码并保存之后你必须退出当前的会话然后重新登录来使改变生效LCTT注你也可以直接使用命令“source~/.bashrc”来让改变生效。每件事情都是交由curl命令处理 它负责将需要解释的命令以及命令选项传送给mankier服务然后将必要的信息打印到Linux命令行。不必说的就是使用这个工具你总是需要连接网络。
让我们用explain.sh脚本测试几个笔者不懂的命令例子。
**1.我忘了du -h是干嘛用的 我只需要这样做:**
$ explain 'du -h'
![Get Help on du Command](http://www.tecmint.com/wp-content/uploads/2015/07/Get-Help-on-du-Command.png)
获得du命令的帮助
**2.如果你忘了'tar -zxvf'的作用,你可以简单地如此做:**
$ explain 'tar -zxvf'
![Tar Command Help](http://www.tecmint.com/wp-content/uploads/2015/07/Tar-Command-Help.png)
Tar命令帮助
**3.我的一个朋友经常对'whatis'以及'whereis'命令的使用感到困惑,所以我建议他:**
在终端简单的地敲下explain命令进入交互模式。
$ explain
然后一个接着一个地输入命令,就能在一个窗口看到他们各自的作用:
Command: whatis
Command: whereis
![Whatis Whereis Commands Help](http://www.tecmint.com/wp-content/uploads/2015/07/Whatis-Whereis-Commands-Help.png)
Whatis/Whereis命令的帮助
你只需要使用“Ctrl+c”就能退出交互模式。
**4. 你可以通过管道来请求解释更多的命令。**
$ explain 'ls -l | grep -i Desktop'
![Get Help on Multiple Commands](http://www.tecmint.com/wp-content/uploads/2015/07/Get-Help-on-Multiple-Commands.png)
获取多条命令的帮助
同样地你可以请求你的shell来解释任何shell命令。 前提是你需要一个可用的网络。输出的信息是基于解释的需要从服务器中生成的,因此输出的结果是不可定制的。
对于我来说这个工具真的很有用并且它已经荣幸地添加在我的.bashrc文件中。你对这个项目有什么想法它对你有用么它的解释令你满意吗请让我知道吧
请在下面评论为我们提供宝贵意见,喜欢并分享我们以及帮助我们得到传播。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/explain-shell-commands-in-the-linux-shell/
作者:[Avishek Kumar][a]
译者:[dingdongnigetou](https://github.com/dingdongnigetou)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/avishek/
[1]:http://www.tecmint.com/cheat-command-line-cheat-sheet-for-linux-users/