RedHat Enterprise Linux Computing Essentials 总揽之运行命令


 

四、运行命令

主要概念

  1. 像任何语言一样,bash shell 使用一种特定的语法。
  2. 任何一个命令行的第一个词都是要运行的程序的名称。
  3. 可用命令行选项(通常是任选的)修改命令的行为,命令行选项总是用一个或两个连字符开头(- 或 –)。
  4. 跟随命令和任何命令行选项的词叫做命令的“参数(argument)”。
  5. 有些命令行选项也带有参数。
  6. 命令通常支持命令行选项 –help、-h 和-?,这些命令行选项给出如何使用命令的概要。

讨论

 

命令行语法
命令行界面有许多优点,包括高效和灵活,但使用起来并不简单。幸好,(几乎)所有的程序都遵循惯例。熟悉了这些惯例,学习新的程序就会容易很多。

将输入命令行的命令和英文句子作比较,我们会说命令有动词,副词和直接宾语。动词是要运行的命令,副词是可以修改命令(“静默形式”或“详尽执 行”)行为的各种命令行选项,剩余的词是直接宾语(命令执行的对象)。和语言一样,命令行语言有不规则形式,而且任何规则都有其例外。

命令
命令的第一个词一般是位于文件系统某个位置的、以文件形式存在的一个程序的名称。[1]比如说,在前面的章节中所使用的命令 ps 和 who。如果想知道哪个文件含有这些程序,可以使用一个叫做 which 的程序。先输入 which命令,接着输入另一个命令的名称,就会发现运行的是哪个文件。

Source code

 

[elvis@station elvis]$ which ps

/bin/ps

[elvis@station elvis]$ which who

/usr/bin/who

 

运行命令时,shell 进程指示内核把指定的程序作为另一个进程分开执行,并将进程的输出(或更准确的说,标准输出流)写入终端。然后shell 暂停,等待命令的进程结束运行。一旦命令结束,shell 会给出另一个提示,等待下个行动。

命令行选项
许多命令可以指定不同的命令行选项来修改它们的行为,列出目录内容的ls 命令就是这样一个简单的例子。看以下ls 命令的三种使用方法,每种用法都列出目录/usr 中的文件;

Source code

 

[elvis@station elvis]$ ls /usr

bin   etc    include   lib      local  share  tmp

dict  games  kerberos  libexec  sbin   src    X11R6

 

[elvis@station elvis]$ ls -s /usr

total 132

  40 bin      4 games       40 lib         8 sbin      0 tmp

   4 dict     8 include      4 libexec     8 share     4 X11R6

   4 etc      4 kerberos     0 local       4 src

 

[elvis@station elvis]$ ls -l /usr

total 132

drwxr-xr-x    2 root     root        40960 Apr 25 06:36 bin

drwxr-xr-x    2 root     root         4096 Jan 24 18:52 dict

drwxr-xr-x    2 root     root         4096 Jan 24 18:52 etc

drwxr-xr-x    4 root     root         4096 Jan 24 18:52 games

drwxr-xr-x  100 root     root         8192 Apr 11 05:55 include

drwxr-xr-x    8 root     root         4096 Mar 31 21:52 kerberos

drwxr-xr-x   90 root     root        40960 Apr 25 06:36 lib

drwxr-xr-x   10 root     root         4096 Apr 11 05:51 libexec

lrwxrwxrwx    1 root     root           14 Sep 13  2002 local -> ../home/local/

drwxr-xr-x    2 root     root         8192 Apr 25 06:36 sbin

drwxr-xr-x  212 root     root         8192 Apr 23 16:30 share

drwxrwxr-x    5 root     pst          4096 Apr 25 08:12 src

lrwxrwxrwx    1 root     root           10 Apr  1 11:07 tmp -> ../var/tmp

drwxr-xr-x    8 root     root         4096 Jan 24 18:52 X11R6

 

第一个ls 命令只列出目录的内容。第二个命令ls -s 包括命令行选项-s,给出内容的大小。第三个命令ls -l 显示具体列表,包括文件的各种信息,如所有者、权限和修改次数。我们现在先不必考虑输出的具体内容,这将在下一本关于使用文件系统的教程中讨论。我们现在 只需要知道命令行选项可以改变命令ls 的基本行为。

短命令行选项
注意,以上用到的两个命令行选项 -s 和 -l 都是单字母选项,这被称为“短”命令行选项。有时候,短命令行选项也可以带有参数。比如说,ls 命令带有命令行选项 -w,这个选项指定了输出的“宽度(字符个数)”。我们来看下面的例子。

Source code

 

[elvis@station elvis]$ ls -w 40 /usr/

bin   games     lib      sbin   tmp

dict  include   libexec  share  X11R6

etc   kerberos  local    src

 

在这里,40 不是ls 命令的参数,而是命令行选项-w 的参数。(输出的宽度应该是多少?40个字符。)命令行选项参数在命令行选项之后。你怎么知道哪个命令行选项带有参数,哪个没有呢?根据经验判断,我们也会很快谈到寻求帮助的方法。

多个短命令行选项
可以同时使用多个命令行选项。多个命令行选项会串在一起,挤在命令和命令参数之间。下面的例子介绍了 ls 命令的一个新的命令行选项 -r,它反转了排序的顺序。我们来看看它如何与选项 -s 和 -w 一起使用。

Source code

 

[elvis@station elvis]$ ls -s -w 40 -r /usr/

total 132

   4 X11R6     0 local        4 games

   0 tmp       4 libexec      4 etc

   4 src      40 lib          4 dict

   8 share     4 kerberos    40 bin

   8 sbin      8 include

有时,在使用多个命令行选项时,用户可以用一个捷径把所有的选项“串”到一个连字符上(-),如下例。

Source code

 

[elvis@station elvis]$ ls -srw 40  /usr/

total 132

   4 X11R6     0 local        4 games

   0 tmp       4 libexec      4 etc

   4 src      40 lib          4 dict

   8 share     4 kerberos    40 bin

   8 sbin      8 include

所有不带参数的单字符选项(此处是-s 和-r)可以串在一起,共用一个 – 。如果选项带有参数,如-w 40,只有将它放在最后,它才能和其它选项共用一个连字符。这样的话,可以在命令行上接着指定它的参数。

长命令行选项
在早期的Unix 中,所有的命令行选项都使用以上语法。 随着Unix 的发展,人们开始意识到他们需要“长”命令行选项。与单字符选项不同的是,长选项由词组成。长选项不是用一个连字符开头,而是用两个连字符(–)开头。 有些命令只使用短选项,有些使用长选项。另外一些命令,如ls,两种都可以使用。

Source code

 

[elvis@station elvis]$ ls --size /usr/

total 132

  40 bin      4 games       40 lib         8 sbin      0 tmp

   4 dict     8 include      4 libexec     8 share     4 X11R6

   4 etc      4 kerberos     0 local       4 src

长命令行选项带有参数时,其语法会稍有不同。在这种情况下,参数不是作为一个分开的词跟在选项后面,而是和选项连在一起,由= 隔开,如 –width=40。注意,短命令行选项和长命令行选项可以混合使用。

 

[elvis@station elvis]$ ls --size /usr/

total 132

  40 bin      4 games       40 lib         8 sbin      0 tmp

   4 dict     8 include      4 libexec     8 share     4 X11R6

   4 etc      4 kerberos     0 local       4 src

 

长命令行选项带有参数时,其语法会稍有不同。在这种情况下,参数不是作为一个分开的词跟在选项后面,而是和选项连在一起,由= 隔开,如 –width=40。注意,短命令行选项和长命令行选项可以混合使用。

Source code

 

[elvis@station elvis]$ ls --width=40 --size -r  /usr/

total 132

   4 X11R6     0 local        4 games

   0 tmp       4 libexec      4 etc

   4 src      40 lib          4 dict

   8 share     4 kerberos    40 bin

   8 sbin      8 include

参数
与命令行选项相比,参数相对简单。跟在命令名后和命令行选项后的任何词,叫做命令的参数。命令是否需要参数,或需要什么样的参数,取决于命令本身。举例来 说,如果在ls 命令后加带参数,该命令会把参数作为文件或目录列出。ps 命令不需要参数。cal 命令可以不带参数,或带1 到2 个参数,一个月份一个年份。学习程序需要哪些参数以及它如何处理参数,是学习如何使用命令的一部分。

得到帮助:用法
如何记住所有的命令行选项呢?你不需要全部记忆。有经验的Linux 用户都知道如何找到适用的选项。大多数命令都支持长命令行选项 –help,或短命令行选项 -h 和 -?。这些选项通常会指示命令提供“使用(usage)”信息,而不是执行常规的操作。使用信息含有一个关于所需参数、所支持的命令行选项及其意义的概要。注意,ls 命令生成的用信息相当长,在下面的输出中被缩短了。

Source code

 

[elvis@station elvis]$ ls --help

Usage: ls [OPTION]... [FILE]... 1

List information about the FILEs (the current directory by default).

Sort entries alphabetically if none of -cftuSUX 2 nor --sort.

 

Mandatory arguments to long options are mandatory for short options too.

  -a, --all                  do not hide entries starting with .

  -A, --almost-all           do not list implied . and ..

      --author               print the author of each file

  -b, --escape               print octal escapes for non-graphic characters

 

   ...

 

  -k                         like --block-size=1K

  -l                         use a long listing format

 

  ...

 

  -r, --reverse              reverse order while sorting

  -R, --recursive            list subdirectories recursively

  -s, --size  3             print size of each file, in blocks

 

  ...

 

  -v                         sort by version

  -w, --width=COLS 4         assume screen width instead of current value

  -x                         list entries by lines instead of by columns

 

  ...

用法信息中要注意的几点:

  1. 任选的部分在方括号中( ])。
  2. 此处,用法信息是指使用缩写来表示多个短命令行选项。
  3. 注意:ls 支持这个命令行选项的短(-s)和长(–size)两种形式。
  4. 此处是 -w 或 –width 命令行选项,需要参数。

用法信息并不提供命令的完整参考,只是给你足够的信息帮助记忆。在这本教程后面的章节中我们会讨论更多得到帮助的方式。

Examples

学习使用cat 命令
用户madonna 的朋友告诉她,可以使用cat 命令查看文件内容。她以前从没用过这个命令,想学习如何用它。她先查看了这个命令的用法信息。

Source code

 

[madonna@station madonna]$ cat --help

Usage: cat [OPTION] [FILE]...

Concatenate FILE(s), or standard input, to standard output.

 

  -A, --show-all           equivalent to -vET

  -b, --number-nonblank    number nonblank output lines

  -e                       equivalent to -vE

  -E, --show-ends          display $ at end of each line

  -n, --number             number all output lines

  -s, --squeeze-blank      never more than one single blank line

  -t                       equivalent to -vT

  -T, --show-tabs          display TAB characters as ^I

  -u                       (ignored)

  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB

      --help     display this help and exit

      --version  output version information and exit

 

With no FILE, or when FILE is -, read standard input.

 

Report bugs to <bug-coreutils@gnu.org>.

她对这些用法信息并不完全理解,比如说里面谈到的标准输入和输出,但她明白第一行,知道cat 用文件名作为参数。她试着显示文件/etc/anacrontab 的内容。

Source code

 

[madonna@station madonna]$ cat /etc/anacrontab

# /etc/anacrontab: configuration file for anacron

 

# See anacron(8) and anacrontab(5) for details.

 

SHELL=/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

 

1       65      cron.daily              run-parts /etc/cron.daily

7       70      cron.weekly             run-parts /etc/cron.weekly

30      75      cron.monthly            run-parts /etc/cron.monthly

在看了用法信息列出的几个命令行选项时,她注意到命令行选项 -n 标出输出行的行数。她试着使用这个选项。

Source code

 

[madonna@station madonna]$ cat -n /etc/anacrontab

     1  # /etc/anacrontab: configuration file for anacron

     2

     3  # See anacron(8) and anacrontab(5) for details.

     4

     5  SHELL=/bin/sh

     6  PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

     7

     8  1       65      cron.daily              run-parts /etc/cron.daily

     9  7       70      cron.weekly             run-parts /etc/cron.weekly

    10  30      75      cron.monthly            run-parts /etc/cron.monthly

从输出可以容易地看出文件包含10 行,或着容易地查看第6 行。她怀疑从第8 行到第10 行的词之间的空格其实是tab 字符。用法信息中说命令行选项 -t 会用^I 代替任何tab 字符,她试着证实自己的怀疑。

Source code

 

[madonna@station madonna]$ cat -t /etc/anacrontab

# /etc/anacrontab: configuration file for anacron

 

# See anacron(8) and anacrontab(5) for details.

 

SHELL=/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

 

1^I65^Icron.daily^I^Irun-parts /etc/cron.daily

7^I70^Icron.weekly^I^Irun-parts /etc/cron.weekly

30^I75^Icron.monthly^I^Irun-parts /etc/cron.monthly

她从用法信息中得知,命令行选项 -A“和 -vET 一样”,她认为这是短命令行选项 -v-E 和 -T 的快捷结合。她把两者都试验了一下,看自己的理解是否正确。

Source code

 

[madonna@station madonna]$ cat -A /etc/anacrontab

# /etc/anacrontab: configuration file for anacron$

$

# See anacron(8) and anacrontab(5) for details.$

$

SHELL=/bin/sh$

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin$

$

1^I65^Icron.daily^I^Irun-parts /etc/cron.daily$

7^I70^Icron.weekly^I^Irun-parts /etc/cron.weekly$

30^I75^Icron.monthly^I^Irun-parts /etc/cron.monthly$

 

[madonna@station madonna]$ cat -vET /etc/anacrontab

# /etc/anacrontab: configuration file for anacron$

$

# See anacron(8) and anacrontab(5) for details.$

$

SHELL=/bin/sh$

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin$

$

1^I65^Icron.daily^I^Irun-parts /etc/cron.daily$

7^I70^Icron.weekly^I^Irun-parts /etc/cron.weekly$

30^I75^Icron.monthly^I^Irun-parts /etc/cron.monthly$

她看到完全一样的输出,确定自己对使用信息的理解是正确的。

问题
命令行语法

A title
touch
 命令用来更新指定文件上的时间戳。使用以下用法信息和touch 命令的调用示例回答后面六个问题。

Source code

 

[madonna@station madonna]$ which touch

/bin/touch

 

[madonna@station madonna]$ touch --help

Usage: touch [OPTION]... FILE...

Update the access and modification times of each FILE to the current time.

 

Mandatory arguments to long options are mandatory for short options too.

  -a                     change only the access time

  -B SEC, --backward=SEC date back SEC seconds

  -c, --no-create        do not create any files

  -d, --date=STRING      parse STRING and use it instead of current time

  -F SEC, --forward=SEC  date forward SEC seconds

  -f                     (ignored)

  -m                     change only the modification time

  -r, --reference=FILE   use this file's times instead of current time

  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time

  --time=WORD            set time given by WORD: access atime use (same as -a)

                           modify mtime (same as -m)

      --help     display this help and exit

      --version  output version information and exit

 

[madonna@station madonna]$ touch -r /etc/services

touch: file arguments missing

Try `touch --help' for more information.

 

[madonna@station madonna]$ tooch /etc/services

-bash: tooch: command not found

 

[madonna@station madonna]$ touch -k /etc/services

touch: invalid option -- k

Try `touch --help' for more information.

 

[madonna@station madonna]$ touch -r /etc/services /tmp/foo

Question 1
以下哪一项是对touch 命令的合法调用?

  1. A.touch -k /etc/services
  2. B.touch -ac /etc/services
  3. C.touch -nocreate /etc/services
  4. D.touch -t

Question 2
以下哪一项是对touch 命令的合法调用?

  1. A.touch -frm /etc/services
  2. B.touch –cm /etc/services
  3. C.touch –no-create -a /etc/services
  4. D.touch

Question 3
以下哪一项可以最好地解释,为什么madonna 使用touch -r /etc/services 的时候会得到“file arguments missing(缺少文件参数)”的错误信息?

  1. A.The -r command line switch requires an argument, which madonna didn’t supply.
  2. B.文件/etc/services 不存在。
  3. C.系统中没有touch 这个命令。
  4. D.命令行选项-r 不被支持。

Question 4
以下哪一项可以最好地解释,为什么madonna 使用tooch /etc/services 的时候会得到“command not found(找不到命令)”的错误信息?

  1. A.文件/etc/services 不存在。
  2. B.madonna misspelled the command name, and no command named tooch could be found.
  3. C.命令touch 必须带有命令行选项-a。
  4. D.madonna 没有权限执行这个命令。

Question 5
以下哪一项可以最好地解释,为什么madonna 使用touch -k /etc/services 的时候会得到“invalid option(选项无效)”的错误信息。

  1. A.文件/etc/services 不存在。
  2. B.madonna 把命令名拼错了,找不到命令名为touch 的命令。
  3. C.命令touch 必须带有命令行选项-a。
  4. D.The touch command doesn’t support the -k command line switch.

Question 6
当madonna 运行命令touch -r /etc/services /tmp/foo 时,/etc/services 的作用是什么?

  1. A.它是命令touch 的参数。
  2. B.The word serves as an argument to the -r command line switch.
  3. C.它是要运行的命令的名称。
  4. D.拼错了,导致命令无法运行。

Question 7
当用户从命令行上运行who 命令时,以下哪一项能够最好地描述出现的情况?

  1. A.The shell asks the kernel to execute the contents of the file /usr/bin/who as a separate process, displaying the process’s output on the terminal.
  2. B.shell 进行who 的系统调用,直接向Linux 内核要输出。
  3. C.shell 退出,由进程who 代替。进程who 结束后,由新的shell 进程代替。

Question 8
使用长命令行选项时,为什么在选项前用双连字符(如 –size),而不使用单连字符(如-size)?

  1. A.长命令行选项的发明者非常喜欢打字。
  2. B.The word -size could be interpreted as a shortcut for the -s -i -z -e short command line switches, instead of a single long command line switch.
  3. C.单连字符表示强制的命令行选项参数。

D.长命令行选项的发明者喜欢把事情复杂化。

相关内容

    暂无相关文章