Use commands

Up to this point,we have seen a series of mysterious commands,each with its own mysterious options and arguments.In this chapter,we will attempt(试图) to remove some of that mystery and even create some of our own commands.The commands introduced in this chapter are:

  • type - Indicate(标示,指示,指出,象征) how a command name is interpreted(解释)
  • which - Display which executable program will be executed
  • man - Display a command's manual page
  • apropos(恰当的) - Display a list of appropriate(适当的,恰当的) commands
  • info - Display a command's info entry(入口)
  • whatis - Display a very brief description of a command
  • alias - create an alias for a command

 

What  is command?

A command can be one of four different things:

1.An executable program like all those files we saw in /usr/bin.Within this category(各类、类别),programs can be compiled(编译的)binaries such as programs written in C and C++,or programs written in scripting languages such as the shell,perl,python,ruby,etc.

2.A command built into the shell itself.bash supports a number of commands internally called shell builtins.The cd command,for example,is a shell builtin.

3.A shell function.These are miniature shell scripts incorporated(包含,把...合并) into the environment.We will cover configuring the environmet and writing shell functions in later chapters,but for now,just be aware that they exist.

4.An alias.Commands that we can define ourselves,built from other commands.

 

Distingulish command

It is often useful to know exactly which of the four kinds of commands is being used and Linux provides a couple of ways to find out.

type - display the type of the command

The type command is a shell builtin that displays the kind of command the shell will execute,given a particular command name.It works like this:

Where "command" is the name of the command you want to examine.Here are some examples:

Here we see the results for three different commands.Notice that the one for ls(taken from a Fedora system但是我这里是RHEL做的) and how the ls command is actually an alias for the ls command with the "--color=tty" option added.Now we know why the output from ls is displayed in color!

 which - display the location of a executable program

Sometimes there is more than one version of  an executable program installed on a system.While this is not very common on desktop systems,it's not unusual on large servers.To determine the exact location of a given executable,the which command is used:

which only works for executable programs,not builtins nor aliases that are subsitutes for actual executable programs.When we try to use which on a shell builtin,for example,cd,we either get no reponse or an error message:(但是不晓得什么鬼,我用RHEL7.0实验的没毛病啊)

which is a fancy way of saying "command not found."

The command and line书上的

 我做的

 

Get command documents

With the knowledge of what a command is,we can now search for the documentation available for each kind of command.

help - the help documention  of get shell builtins

bash has a built -in help facility available for each of the shell builtins.To use it ,type "help" followed by the name of the shell builtin.For example"

A note on notation:When square brackets(方括号) appear in the description of a command's syntax,they indicate optional items.A  vertical(垂直的)bar(棒)(竖杠) character indicates mutually(互相的) exclusive(专用的,独家的) (互斥的)items.In the case of the cd command above:

cd [-L|-P] [dir]

 This notation says that the command cd may be followed optionally by either a "-L" or a "-P" and further,optionally followed by the argument "dir"

 While the output of help for the cd commands is concise(简明的) and accurate(精确的),it is by no means tutorial(辅导的)and as we can see,it also seems to mention a lot of things we haven't talked about yet! Don't worry.We'll get there.

--help-- display the information of usage

Many executable programs support a "--help" option that displays a description of the command's supported syntax(句法,句法规则) and options.For example

Some programs don't support the "--help" option,but try it anyway.Often it results in an error message  that will reveal(揭示) the same usage information.

 

man - 显示程序手册页

Most executable programs intended for command line use provide a formal piece of documentation called a manual or man page.A special paging program called man is used to view them.It is used like this:

where "program" is the name of the command to view.

Man pages vary (变化) somewhat in format but generally contain a title,a synopsis(摘要,大纲) of the command's syntax(句法,句法规则),a description of the command's purpose,and a listing and description of each of the command's options.Man pages,however,do not usually include examples,and are intended as a reference,not a tutorial(教程).As an example,let's try viewing the man page for the ls command:

On most Linux system,man uses less to display the manual page,so all of the familiar less commands work while displaying the page.

The "manual" that man displays is broken into sections and not only covers user commands but also system administration commands, programming interfaces,file formats and more.The table below describes the layout of the manual:

Table 6-1:Man Page Organization

SectionContents
1User commands
2Programming interfaces kernel system calls
3Programming interfaces to the C library
4Special files such as device nodes and drivers
5File formats
6Games and amusements such as screen savers
7Miscellaneous(不同各类的,多种多样的,复杂的。在这指其他方面)
8System administration commands

Sometimes we need to look in a specific section of the manual to find what we are looking for.This is particularly true if we are looking for a file format that is also the name of the a command.Without specifying a section number,we will always get the first instance of a match,probably in section 1.To specify a section number,we use man like this:

For example:

This will display the man page descibing the file format of the /etc/passwd file.

apropos - display apropriate command

It is also possible to search the list of man pages for possible matches based on  a search term(搜索词,关键字).It's very crude but sometimes helpful.Here is an example of a search for man pages using the search term "floppy" 

The first field in each line of output is the name of the man page, the second field shows the section.Note that the man command with the "-k" option performs the exact same function as apropos.

 

whatis - display a very concise command description 

The whatis program displays the name and a one line description of a man page matching a specified keyword:

 

The Most Brutal Man Page Of Them All(最晦涩难懂的手册页)

As we have seen,the manual pages supplied with Linux and other Unix-like systems are intended as reference documentation and not as tutorials.Many man pages are hard to read,but I think that the grand prize for difficulty has got to go to the man page for bash.As I was doing my research for this book,I gave it careful review  to ensure that I was covering most of its topics.When printed,it's over eighty pages long and extremely dense,and its structure makes absolutely no sense to a new user.

On the other hand,it is very accurate and concise,as well as being extremely complete.So check it out if you dare and look forward to the day when you can read it and it all make sense(都搞懂了).

 

info - display program Info entry(显示程序Info条目)

The GNU Project provides an alternative(供替代的) to man pages for their programs,called "info." Info pages are displayed with a reader program named,appropriately enough,info. Info pages are hyperlinked much like web pages.Here is a sample:

The info program reads info files,which are tree structured into individual nodes, each containing a single topic.Info files contain hyperlinks that can move you from node to node.A hyperlink can be identified by its leading asterisk(星号,注上星号,用星号标出),and is activated by placing the cursor (光标) upon it and pressing the enter key.

To invoke info,type "info" followed optionally by the name of a program.Below is a table of commands used to control the reader while displaying an info page:

CommandAction
?Display command help
PgUp or BackspaceDisplay previous page
PgDn or SpaceDisplay next page
nNext - Display the next node
pPrevious - Display the previous nod
uUp - Display the parent node of the currently displayed node,usually a menu.
EnterFollow the hyperlink at the cursor location
qQuit

Most of the command line programs we have discussed so far are part of the GNU Project's "coreutils" package,so typing:

will display a menu page with hyperlinks to each program contained in the coreutils package.

 

README和其它程序文档

Many software packages installed on your system have documentation files residing in the /usr/share/doc directory.Most of these are stored in plain text format and can be viewed with less.Some of the files are in HTML format and can be viewed with a web browser.We may encounter some files ending with a ".gz" extension.This indicates that they have been compressed with the gzip compression program.The gzip package includes a special version of less called zless that will display the contents of gzip-compressed text files.

 

Create your command with alias

Now for  our very first experience with programming! We will create a command of our own using the alias command.But before we start,we need to reveal a small command line trick.It's possible to put more than one command  on a line by separating each command with a semicolon (分号) character.It works like this:

Here's the example we will use:

As we can see,we have combined three commands on one line.First we change directory to /usr then list the directory and finally retrun to the original directory(by using 'cd -') so we end up where we started.Now let's turn this sequence into a new command using alias.The first thing we have to do is dream up a name for our new command.Let's try "test".Before we do that,it would be a good idea to find out if the name "test" is already being used.To find out,we can use the type command again:

Oops! The name "test" is already taken.Let's try "foo":

Great!"foo" is not taken.So let's create our alias:

  Notice the structure of this command:

After the command "alias" we give alias a name followed immediately (no whitespace allowed) by an equals sign,followed immediately by a quoted string containning the meaning to be assigned to the name.After we define our alias,it can be used anywhere the shell would expect a command.Let's try it:

 

We can also use the type command again to see our alias:

To remove an alias,the unalias command is used,like so:

While we purposefully avoided naming our alias with an existing command name,it is not uncommon to do so.This is often done to apply a commonly desired option to each invocation(乞求) of a common command.For instance,we saw earlier how the ls command is often aliased to add color support:

To see all the aliases defined in the environment,use the alias command without arguments.Here are some of the aliases defined by default on a Fedora system.Try and figure out what they all do:

There is one tiny problem with defining aliases on the command line.They vanish(消失) when your shell session ends.In  a later chapter,we will see how to add our own aliases to the files that establish the environment each time we log on,but for now,enjoy the fact that we have taken our first, albeit(即使,虽然) tiny, step into the world of shell programming!

 

转载于:https://www.cnblogs.com/itmeatball/p/7518397.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值