特殊:Badtitle/NS100:CommandlineHowto/zh:修订间差异
无编辑摘要 |
无编辑摘要 |
||
第13行: | 第13行: | ||
=== 命令行的历史 === | === 命令行的历史 === | ||
在计算机出现的早期,命令行是唯一的人机交互方式。后来,'''Xerox's Palo Alto 研究中心(PARC)''' 提出了图形用户界面(GUI)的概念,以后的大多数 GUI 都是基于它的概念构建的。后来,苹果花了一大笔钱,才被允许“研究”他们自己的 GUI。苹果拥有了自己的 GUI 后,直到 1986 年,Unix 上终于出现了第一个 GUI,它被命名为 X。不过 Linux 不得不再等了 10 年,才发布 XFree86。它至今仍是当初 X server 的免费实现版本。 | |||
之前我介绍过,CLI(命令行界面)是图形界面出来之前唯一的人机交互途径。1969 年,'''贝尔电话实验室''' 发布了 UNIX Timeshare System 的 V1 版本。UNIX 有了一个 shell,称为 | 之前我介绍过,CLI(命令行界面)是图形界面出来之前唯一的人机交互途径。1969 年,'''贝尔电话实验室''' 发布了 UNIX Timeshare System 的 V1 版本。UNIX 有了一个 shell,称为 sh。很长一段时间内它都是同计算机进行交流的唯一渠道。到了后来,UNIX 出现了衍生版本:HP-UX,1BSD,Solaris,OpenVMS,IRIX,SCO XENIX,等等。再接着,Linux 出现了。Linux 的发展历程同本文关系不大,总之 sh 之外的 CLI 开始多了起来:zsh,ksh,bourne shell,等等。 | ||
==== POSIX ==== | ==== POSIX ==== | ||
第39行: | 第39行: | ||
=== 如何执行命令 === | === 如何执行命令 === | ||
依据本文的目标,我们将在桌面上打开命令行界面。 | 依据本文的目标,我们将在桌面上打开命令行界面。 <br> '''Ubuntu''' <br> ''Applications | Accessories | Terminal'' <br> '''Kubuntu''' <br> ''KDE Menu | System | Konsole Terminal Program'' <br> '''Xubuntu''' ''6.10'' <br> ''Applications | System | Terminal''<br> | ||
<br> '''Ubuntu''' | |||
<br> ''Applications | Accessories | Terminal'' | |||
<br> '''Kubuntu''' | |||
<br> ''KDE Menu | System | Konsole Terminal Program'' | |||
<br> '''Xubuntu''' ''6.10'' | |||
<br> ''Applications | System | Terminal''<br> | |||
=== | === 基本格式和概念 === | ||
你首先注意到的应该是下面的文字: | |||
<pre>dud@shadowplay:~ $ | <pre>dud@shadowplay:~ $ | ||
或者 | |||
[dud@shadowplay ~]$ | [dud@shadowplay ~]$ | ||
</pre> | </pre> | ||
你所看到的这叫做''提示符''。它表明计算机已经准备好了,并且在等待用户的输入。在这个例子当中,'''dud''' 指的是我当前登录的用户名,'''shadowplay''' 是计算机的名字,'''~''' 则表示当前的路径(用户的主目录)。基本概念如下: | |||
* | |||
* | * '''终端'''指的是 Linux 操作系统的“物理(直接)”界面。 | ||
* | * 我们现在用的叫做'''终端仿真器''',也就是在 GUI 界面中模拟一个 CLI 界面。当它关闭时,通过这个仿真器中运行起来的所有程序也都会终止。 | ||
* | * '''shell''' 是终端下的一个命令翻译器。 | ||
* | * '''命令'''是 shell 将会为你执行的东西。 | ||
* | * 命令执行返回的结果称为'''输出''',执行结果通常输出到终端上。 | ||
* 命令的'''输入'''指的是这个命令要用到的参数或数据。命令会根据输入来调整自己的执行方式。 | |||
* 一个正在运行的程序成为'''进程'''。它可以被激活、休眠,或者变成其他状态。 | |||
== 命令语法 == | == 命令语法 == |
2008年2月16日 (六) 14:11的版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/CommandlineHowto }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/af | • {{#if: UbuntuHelp:CommandlineHowto|Afrikaans| [[::CommandlineHowto/zh/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/ar | • {{#if: UbuntuHelp:CommandlineHowto|العربية| [[::CommandlineHowto/zh/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/az | • {{#if: UbuntuHelp:CommandlineHowto|azərbaycanca| [[::CommandlineHowto/zh/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/bcc | • {{#if: UbuntuHelp:CommandlineHowto|جهلسری بلوچی| [[::CommandlineHowto/zh/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/bg | • {{#if: UbuntuHelp:CommandlineHowto|български| [[::CommandlineHowto/zh/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/br | • {{#if: UbuntuHelp:CommandlineHowto|brezhoneg| [[::CommandlineHowto/zh/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/ca | • {{#if: UbuntuHelp:CommandlineHowto|català| [[::CommandlineHowto/zh/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/cs | • {{#if: UbuntuHelp:CommandlineHowto|čeština| [[::CommandlineHowto/zh/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/de | • {{#if: UbuntuHelp:CommandlineHowto|Deutsch| [[::CommandlineHowto/zh/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/el | • {{#if: UbuntuHelp:CommandlineHowto|Ελληνικά| [[::CommandlineHowto/zh/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/es | • {{#if: UbuntuHelp:CommandlineHowto|español| [[::CommandlineHowto/zh/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/fa | • {{#if: UbuntuHelp:CommandlineHowto|فارسی| [[::CommandlineHowto/zh/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/fi | • {{#if: UbuntuHelp:CommandlineHowto|suomi| [[::CommandlineHowto/zh/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/fr | • {{#if: UbuntuHelp:CommandlineHowto|français| [[::CommandlineHowto/zh/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/gu | • {{#if: UbuntuHelp:CommandlineHowto|ગુજરાતી| [[::CommandlineHowto/zh/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/he | • {{#if: UbuntuHelp:CommandlineHowto|עברית| [[::CommandlineHowto/zh/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/hu | • {{#if: UbuntuHelp:CommandlineHowto|magyar| [[::CommandlineHowto/zh/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/id | • {{#if: UbuntuHelp:CommandlineHowto|Bahasa Indonesia| [[::CommandlineHowto/zh/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/it | • {{#if: UbuntuHelp:CommandlineHowto|italiano| [[::CommandlineHowto/zh/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/ja | • {{#if: UbuntuHelp:CommandlineHowto|日本語| [[::CommandlineHowto/zh/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/ko | • {{#if: UbuntuHelp:CommandlineHowto|한국어| [[::CommandlineHowto/zh/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/ksh | • {{#if: UbuntuHelp:CommandlineHowto|Ripoarisch| [[::CommandlineHowto/zh/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/mr | • {{#if: UbuntuHelp:CommandlineHowto|मराठी| [[::CommandlineHowto/zh/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/ms | • {{#if: UbuntuHelp:CommandlineHowto|Bahasa Melayu| [[::CommandlineHowto/zh/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/nl | • {{#if: UbuntuHelp:CommandlineHowto|Nederlands| [[::CommandlineHowto/zh/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/no | • {{#if: UbuntuHelp:CommandlineHowto|norsk| [[::CommandlineHowto/zh/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/oc | • {{#if: UbuntuHelp:CommandlineHowto|occitan| [[::CommandlineHowto/zh/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/pl | • {{#if: UbuntuHelp:CommandlineHowto|polski| [[::CommandlineHowto/zh/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/pt | • {{#if: UbuntuHelp:CommandlineHowto|português| [[::CommandlineHowto/zh/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/ro | • {{#if: UbuntuHelp:CommandlineHowto|română| [[::CommandlineHowto/zh/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/ru | • {{#if: UbuntuHelp:CommandlineHowto|русский| [[::CommandlineHowto/zh/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/si | • {{#if: UbuntuHelp:CommandlineHowto|සිංහල| [[::CommandlineHowto/zh/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/sq | • {{#if: UbuntuHelp:CommandlineHowto|shqip| [[::CommandlineHowto/zh/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/sr | • {{#if: UbuntuHelp:CommandlineHowto|српски / srpski| [[::CommandlineHowto/zh/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/sv | • {{#if: UbuntuHelp:CommandlineHowto|svenska| [[::CommandlineHowto/zh/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/th | • {{#if: UbuntuHelp:CommandlineHowto|ไทย| [[::CommandlineHowto/zh/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/tr | • {{#if: UbuntuHelp:CommandlineHowto|Türkçe| [[::CommandlineHowto/zh/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/vi | • {{#if: UbuntuHelp:CommandlineHowto|Tiếng Việt| [[::CommandlineHowto/zh/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/yue | • {{#if: UbuntuHelp:CommandlineHowto|粵語| [[::CommandlineHowto/zh/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/zh | • {{#if: UbuntuHelp:CommandlineHowto|中文| [[::CommandlineHowto/zh/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/zh-hans | • {{#if: UbuntuHelp:CommandlineHowto|中文(简体)| [[::CommandlineHowto/zh/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CommandlineHowto | UbuntuHelp:CommandlineHowto | {{#if: | :}}CommandlineHowto/zh}}/zh-hant | • {{#if: UbuntuHelp:CommandlineHowto|中文(繁體)| [[::CommandlineHowto/zh/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:CommandlineHowto|:CommandlineHowto/zh|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :CommandlineHowto/zh/zh | | {{#ifexist: CommandlineHowto/zh/zh | | {{#ifeq: {{#titleparts:CommandlineHowto/zh|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:CommandlineHowto/zh|1|-1|}} | zh | | }}
介绍
Ubuntu 是一个对新手友好的,带有很棒的图形界面的发行版。尽管如此,有时候还是免不了需要大量重复的鼠标操作。其实只需要在键盘上敲几个键就能免除这些繁复。我认为这有百利而无一害:不论你做什么,Linux 都能够在命令行中发挥它真正的力量!:)
什么是命令行?
简单的说,命令行就是以一行一行的命令为基础的用户界面。你可以管它叫 字符型直接串行命令处理器。多数情况下,用户同计算机进行交互的方式是键入一行(或多行)命令。这些命令根据命令处理器的语法,触发了计算机执行各种动作。
为了保持简单易懂,我们一步一步慢慢来。/!\ 你要是等不及,不妨直接跳去看 命令语法 一节。
命令行的历史
在计算机出现的早期,命令行是唯一的人机交互方式。后来,Xerox's Palo Alto 研究中心(PARC) 提出了图形用户界面(GUI)的概念,以后的大多数 GUI 都是基于它的概念构建的。后来,苹果花了一大笔钱,才被允许“研究”他们自己的 GUI。苹果拥有了自己的 GUI 后,直到 1986 年,Unix 上终于出现了第一个 GUI,它被命名为 X。不过 Linux 不得不再等了 10 年,才发布 XFree86。它至今仍是当初 X server 的免费实现版本。
之前我介绍过,CLI(命令行界面)是图形界面出来之前唯一的人机交互途径。1969 年,贝尔电话实验室 发布了 UNIX Timeshare System 的 V1 版本。UNIX 有了一个 shell,称为 sh。很长一段时间内它都是同计算机进行交流的唯一渠道。到了后来,UNIX 出现了衍生版本:HP-UX,1BSD,Solaris,OpenVMS,IRIX,SCO XENIX,等等。再接着,Linux 出现了。Linux 的发展历程同本文关系不大,总之 sh 之外的 CLI 开始多了起来:zsh,ksh,bourne shell,等等。
POSIX
The Wikipedia defines POSIX as the following: "POSIX is the collective name of a family of related standards specified by the IEEE to define the application program interface (API) for software designed to run on variants of the Unix OS. They are formally designated as IEEE 1003 and the international standard name is ISO/IEC 9945. The standards emerged from a project, begun circa 1985. The term POSIX was suggested by Richard Stallman in response to an IEEE request for a memorable name; before that the standards effort was called IEEE-IX. POSIX is a near acronym for Portable Operating System Interface, with the X signifying the Unix heritage of the API." This sounds fancy and all, but to stay rather concise, POSIX is the underlying standard and functionality of how your CLI responds.
使用命令行的优势
So this all sounds very dull and boring, not to mention hard.
Well, it isn't, really. ;) It's quite easy once you understand the basics. 使用命令行的优势在于:
- 节省时间;
- 当系统崩溃或者由于配置问题,而没法使用图形界面时,命令行的作用就体现出来了;
- 某些任务无法在图形界面下完成。
举个例子。有一天系统管理员告诉你,你占用了太多的空间。那么你就要找出自己哪些地方占用的空间最多。在图形界面下,你可以拿出你的秒表,预备——开始!
现在,切换到命令行,执行下面的语句:`du | sort -n`(我们等下再解释)。看到没?命令行下有些事情做起来就是快(虽然图形界面下更容易)。
学习命令行的先决条件
本文假设你安装了任意版本的 Ubuntu,并且有兴趣了解其内在机制。
如何执行命令
依据本文的目标,我们将在桌面上打开命令行界面。
Ubuntu
Applications | Accessories | Terminal
Kubuntu
KDE Menu | System | Konsole Terminal Program
Xubuntu 6.10
Applications | System | Terminal
基本格式和概念
你首先注意到的应该是下面的文字:
dud@shadowplay:~ $ 或者 [dud@shadowplay ~]$
你所看到的这叫做提示符。它表明计算机已经准备好了,并且在等待用户的输入。在这个例子当中,dud 指的是我当前登录的用户名,shadowplay 是计算机的名字,~ 则表示当前的路径(用户的主目录)。基本概念如下:
- 终端指的是 Linux 操作系统的“物理(直接)”界面。
- 我们现在用的叫做终端仿真器,也就是在 GUI 界面中模拟一个 CLI 界面。当它关闭时,通过这个仿真器中运行起来的所有程序也都会终止。
- shell 是终端下的一个命令翻译器。
- 命令是 shell 将会为你执行的东西。
- 命令执行返回的结果称为输出,执行结果通常输出到终端上。
- 命令的输入指的是这个命令要用到的参数或数据。命令会根据输入来调整自己的执行方式。
- 一个正在运行的程序成为进程。它可以被激活、休眠,或者变成其他状态。
命令语法
This section will try to give you a good rundown of the basic usage for the bash shell, which is the default user shell in Ubuntu.
单个命令
The command syntax will vary with each command. Here are some of the basics. The simplest way to use some commands is to type just the command.
command Example:
dud@shadowplay:~ $ ls file1.txt file2.pdf file3.mp3 file1.pdf another_file.txt Yet-Another_file.txt file-with_other-NAME.TXT
The above example displays the contents of the current working directory, while other commands may require one or more arguments. command <argument> Example:
dud@shadowplay:~ $ cat file1.txt Roses are red. Violets are blue, and you have the bird-flu!
The above example shows you the content of the file1.txt file by adding the filename as the argument for the cat command. Almost all commands, whether they have arguments or not, have options. command -option Example:
dud@shadowplay:~ $ ls -r file-with_other-NAME.TXT Yet-Another_file.txt another_file.txt file1.pdf file3.mp3 file2.pdf file1.txt
Using the previous example of showing the current directory, we have added the -r option. As you can see, the listing of the current working directory has been displayed in the reverse order.
多个命令
Sometimes the desired task may require the use of more than one command to be completed. Here is the syntax for the use of multiple commands. If you want to execute two commands consecutively, then you would use the following syntax: command1 ; command2 Example:
dud@shadowplay:~ $grep red file1.txt ; grep blue file1.txt Roses are red, Violets are blue,
In the example above, command1 and command2 are executed. However, if you need command1 to complete successfully before executing command2, then you would use the following syntax: command1 && command2 Example:
dud@shadowplay:~ $ grep red file1.txt && grep blue file1.txt Roses are red, Violets are blue, dud@shadowplay:~ $ grep purple file1.txt && grep blue file1.txt dud@shadowplay:~ $
In the example above, you will notice nothing happened when the first command did not complete successfully. If you want command2 to execute only if command1 fails, then you would use the following syntax: command1 || command2 Example:
dud@shadowplay:~ $ grep red file1.txt || grep blue file1.txt Roses are red, dud@shadowplay:~ $ grep purple file1.txt || grep blue file1.txt Violets are blue, dud@shadowplay:~ $
In the example above, you will notice command2 was only executed when command1 failed.
通配符
There is a powerful feature when using arguments called wildcards, which are single or multiple characters that can represent anything or a specific range of characters. The syntax for the wildcard is as follows: command * Example:
dud@shadowplay:~ $ ls f* file1.txt file2.pdf file3.mp3 file1.pdf file-with_other-NAME.TXT
The example above gives a list of all files that start with the letter f. The next wildcard we will use allows you to match any single character. The syntax for this wildcard is as follows: command ? Example:
dud@shadowplay:~ $ ls file?.pdf file2.pdf
The example above gives you output of only one file that fits the pattern. Finally, the following wildcard allows you to use a range of values, and its syntax is: command [abc...] Example:
dud@shadowplay:~ $ ls file[0-9].* file1.txt file2.pdf file3.mp3 file1.pdf
In the above example, there were two wildcards used: the range wildcard followed by matching any strings wildcard. You can see with the above examples just how powerful using wildcards can be.
控制流
Commands read input from the keyboard (standard input, or stdin) and write to output (standard out, or stdout). There is also a special output category for error messages called standard error (or stderr). These three locations are created automatically for each program. We can redirect input and output to and from a command.
重定向
If you wanted the output of a command to go to a file instead of the terminal, then you would use the following syntax: command > filename Example:
dud@shadowplay:~ $ ls > file4.txt dud@shadowplay:~ $ cat file4.txt file1.txt file2.pdf file3.mp3 file1.pdf another_file.txt Yet-Another_file.txt file-with_other-NAME.TXT file4.txt
The above example will create file4.txt if it is not found. NOTE: If file4.txt exists already, the above command will overwrite its contents. If you want to add to the end of a existing file, then you would use the following syntax: command >> filename Example:
dud@shadowplay:~ $ ls >> file4.txt dud@shadowplay:~ $ cat file4.txt file1.txt file2.pdf file3.mp3 file1.pdf another_file.txt Yet-Another_file.txt file-with_other-NAME.TXT file4.txt file1.txt file2.pdf file3.mp3 file1.pdf another_file.txt Yet-Another_file.txt file-with_other-NAME.TXT file4.txt
In the example, you will notice the file was appended with the new information. Now we are going to do a different redirection: We are going to take the input from a file for the command to be executed. Here is the syntax for this redirection: command < filename Example:
dud@shadowplay:~ $ sort < file4.txt another_file.txt another_file.txt file1.txt file1.txt file2.pdf file2.pdf file3.mp3 file3.mp3 file4.txt file4.txt file-with_other-NAME.TXT file-with_other-NAME.TXT Yet-Another_file.txt Yet-Another_file.txt
As you can see from this example, we used the file4.txt as input into the sort command.
管道
When you need the output from command 1 for the input into command 2, then you would use pipe character '|'. Here is the syntax for the pipe character: command | command Example:
dud@shadowplay:~ $ ls | sort another_file.txt file1.pdf file1.txt file2.pdf file3.mp3 file-with_other-NAME.TXT Yet-Another_file.txt
The above example is using the output from ls as input to the sort command. You will notice the list has been sorted. As you can see, the command line is an easy and powerful way of completing many tasks. If you want more information on using the command line, then look at the further reading section of this document.
进一步的介绍
http://rute.2038bug.com/index.html.gz
BasicCommands has a list of basic commands.
AdvancedCommandlineHowto has some advanced command line features such as scripting.