个人工具

“UbuntuHelp:AdvancedCommandlineHowto”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/AdvancedCommandlineHowto}}
 
{{From|https://help.ubuntu.com/community/AdvancedCommandlineHowto}}
 
{{Languages|UbuntuHelp:AdvancedCommandlineHowto}}
 
{{Languages|UbuntuHelp:AdvancedCommandlineHowto}}
 
 
=== Introduction ===
 
=== Introduction ===
 
 
The purpose of this document is to give you an understanding of some of the advanced features of the command line. This document is going to assume you have read the CommandlineHowto.  See also the excellent documentation listed in references [[1 1]]  
 
The purpose of this document is to give you an understanding of some of the advanced features of the command line. This document is going to assume you have read the CommandlineHowto.  See also the excellent documentation listed in references [[1 1]]  
 
and [[2 2]].
 
and [[2 2]].
 
 
=== Command Navigation ===
 
=== Command Navigation ===
 
 
==== Keyboard shortcuts ====
 
==== Keyboard shortcuts ====
 
 
Here is a list of commonly used keyboard shortcuts using the default shell, bash (/bin/bash):
 
Here is a list of commonly used keyboard shortcuts using the default shell, bash (/bin/bash):
 
 
'''Up Arrow & Down Arrow''': previously used commands in the current session.<br>
 
'''Up Arrow & Down Arrow''': previously used commands in the current session.<br>
 
'''Ctrl-A''': Move cursor to beginning of the current line.<br>
 
'''Ctrl-A''': Move cursor to beginning of the current line.<br>
第23行: 第17行:
 
'''Ctrl-C''': Stop the current running command.<br>
 
'''Ctrl-C''': Stop the current running command.<br>
 
'''Tab''': command completion.<br>
 
'''Tab''': command completion.<br>
 
 
 
==== Command ''history'' ====
 
==== Command ''history'' ====
 
 
The ''history'' command is a handy tool that allows you to view/use commands that you have already used. One advantage of this tool is when you have to use the same command multiple times. This becomes useful when you a troubleshooting an issue with the system or when you are trying to configure a feature in Linux. Unlike the '''Up Arrow''' and '''Down Arrow''' keys, as seen in the previous section of this document, history will store all commands used as opposed to the commands used in the current session of terminal. Here is an example of using the ''history'' command.
 
The ''history'' command is a handy tool that allows you to view/use commands that you have already used. One advantage of this tool is when you have to use the same command multiple times. This becomes useful when you a troubleshooting an issue with the system or when you are trying to configure a feature in Linux. Unlike the '''Up Arrow''' and '''Down Arrow''' keys, as seen in the previous section of this document, history will store all commands used as opposed to the commands used in the current session of terminal. Here is an example of using the ''history'' command.
 
 
<pre><nowiki>
 
<pre><nowiki>
 
localuser@localhost:~ $ history
 
localuser@localhost:~ $ history
第41行: 第31行:
 
localuser@localhost:~ $
 
localuser@localhost:~ $
 
</nowiki></pre>
 
</nowiki></pre>
 
 
As you can see in the example above, just the ''history'' command will list the last used commands. Also in the example above you are able to execute a previous command by using ''!'' followed by the command number.  ''!!'' alone will execute the most recent command again.  
 
As you can see in the example above, just the ''history'' command will list the last used commands. Also in the example above you are able to execute a previous command by using ''!'' followed by the command number.  ''!!'' alone will execute the most recent command again.  
 
 
=== Miscellaneous Command Line Functions ===
 
=== Miscellaneous Command Line Functions ===
 
 
Here are a couple of miscellaneous functions to help use the command line easier.
 
Here are a couple of miscellaneous functions to help use the command line easier.
 
 
==== Environment Variables ====
 
==== Environment Variables ====
 
 
The environment variables are global variables that can be used by all commands. Some of the information stored in the variables is common information as well as system information. An example of an environment variable is '''PATH''', which contains the location of all executables. Using the ''env'' command allows you to list as well as modify the environment variables. When you use just the ''env'' command alone it will list all the environment variables. Here is an example on how to modify an environment variable:
 
The environment variables are global variables that can be used by all commands. Some of the information stored in the variables is common information as well as system information. An example of an environment variable is '''PATH''', which contains the location of all executables. Using the ''env'' command allows you to list as well as modify the environment variables. When you use just the ''env'' command alone it will list all the environment variables. Here is an example on how to modify an environment variable:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
localuser@localhost:~ $ env HOME=/usr/users/pmd bash
 
localuser@localhost:~ $ env HOME=/usr/users/pmd bash
 
</nowiki></pre>
 
</nowiki></pre>
 
 
This example will change your home directory before starting a subshell.
 
This example will change your home directory before starting a subshell.
 
 
==== ''alias'' Command ====
 
==== ''alias'' Command ====
 
 
The ''alias'' command allows you to define a new command. Here is an example of using the ''alias'' command:
 
The ''alias'' command allows you to define a new command. Here is an example of using the ''alias'' command:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
localuser@localhost:~ $ alias l='ls -r'
 
localuser@localhost:~ $ alias l='ls -r'
 
</nowiki></pre>
 
</nowiki></pre>
 
 
This example will allow you to use ''l'' if you want to see the directory in reverse order.
 
This example will allow you to use ''l'' if you want to see the directory in reverse order.
 
 
=== Scripting ===
 
=== Scripting ===
 
Scripting is the combining of commands into a logical file to complete a specific task. Example of scripts would be a login script that allows you to be able to start programs or maybe setup variables that are used only by that user.
 
Scripting is the combining of commands into a logical file to complete a specific task. Example of scripts would be a login script that allows you to be able to start programs or maybe setup variables that are used only by that user.
 
Assumed knowledge for this page is that you can use an editor.
 
Assumed knowledge for this page is that you can use an editor.
 
 
==== Editors ====
 
==== Editors ====
 
 
Within the Linux world, much debate has been had on the topic of which is the best editor. Each has their flaws and their advantages, but the only way to determine which is the one for you is to try them, find one you like, and learn how to use it properly. Here is a list of the most commonly used editors. Links have been provided below for additional information about each of the editors.
 
Within the Linux world, much debate has been had on the topic of which is the best editor. Each has their flaws and their advantages, but the only way to determine which is the one for you is to try them, find one you like, and learn how to use it properly. Here is a list of the most commonly used editors. Links have been provided below for additional information about each of the editors.
 
 
 
* '''Emacs''' Emacs is described by the GNU project  as "the extensible, customizable, self-documenting real-time display editor." Part of Richard Stallman's GNU project, emacs often suits the newcomer, but has hidden depths which require careful study to release the editor's full capabilities. '''''Note:''''' emacs is not included in a standard Ubuntu installation, and must be installed from the Universe Repository. [http://www.gnu.org/software/emacs/ Emacs Homepage]<br>
 
* '''Emacs''' Emacs is described by the GNU project  as "the extensible, customizable, self-documenting real-time display editor." Part of Richard Stallman's GNU project, emacs often suits the newcomer, but has hidden depths which require careful study to release the editor's full capabilities. '''''Note:''''' emacs is not included in a standard Ubuntu installation, and must be installed from the Universe Repository. [http://www.gnu.org/software/emacs/ Emacs Homepage]<br>
 
 
* '''Vim-gnome''' Vim-gnome is the version of the vim editor provided by Ubuntu. The homepage describes Vim as "an advanced text editor that seeks to provide the power of the de-facto Unix editor 'Vi', with a more complete feature set." Unlike most other editors, Vim is a dual-mode editor, where one mode is used for document editing, while a different mode is used to actually insert text. This can seem daunting to new users, but, just like emacs, the editor has powerful features which need to be learned. '''''Note:''''' Vim-gnome is not installed by default in Ubuntu, but it is available from the Installation CD. [http:www.vim.org/ Vim Homepage]<br>
 
* '''Vim-gnome''' Vim-gnome is the version of the vim editor provided by Ubuntu. The homepage describes Vim as "an advanced text editor that seeks to provide the power of the de-facto Unix editor 'Vi', with a more complete feature set." Unlike most other editors, Vim is a dual-mode editor, where one mode is used for document editing, while a different mode is used to actually insert text. This can seem daunting to new users, but, just like emacs, the editor has powerful features which need to be learned. '''''Note:''''' Vim-gnome is not installed by default in Ubuntu, but it is available from the Installation CD. [http:www.vim.org/ Vim Homepage]<br>
 
 
* '''[[UbuntuHelp:Nano|nano]]''' The homepage proudly declares that "GNU nano is a small and friendly text editor." Unlike emacs or vim, there is very little to learn about nano, you can type scripts, you can find/replace, you can get started quickly. It does lack some(most) of the features needed by dedicated coders, but it has its own strengths - most especially its tiny size and memory requirements, which mean that it will almost always be installed on any UNIX-based environment. GNU nano is, of course, installed as part of your Ubuntu installation.  [http://www.nano-editor.org/ Nano Homepage]<br>
 
* '''[[UbuntuHelp:Nano|nano]]''' The homepage proudly declares that "GNU nano is a small and friendly text editor." Unlike emacs or vim, there is very little to learn about nano, you can type scripts, you can find/replace, you can get started quickly. It does lack some(most) of the features needed by dedicated coders, but it has its own strengths - most especially its tiny size and memory requirements, which mean that it will almost always be installed on any UNIX-based environment. GNU nano is, of course, installed as part of your Ubuntu installation.  [http://www.nano-editor.org/ Nano Homepage]<br>
 
 
==== ScriptingHowto ====
 
==== ScriptingHowto ====
 
 
 
Each time you type on the command line you can save what you are doing and repeat it any number of times by creating a file called a script. You can simply edit a series of repeated commands into a file.  
 
Each time you type on the command line you can save what you are doing and repeat it any number of times by creating a file called a script. You can simply edit a series of repeated commands into a file.  
 
 
Edit a file called reverse and place the following lines in it:
 
Edit a file called reverse and place the following lines in it:
 
<pre><nowiki>
 
<pre><nowiki>
第101行: 第69行:
 
</nowiki></pre>
 
</nowiki></pre>
 
You should see the directory listing in reverse. There are, of course, many simpler ways to achieve this result; this is meant as a simple example.
 
You should see the directory listing in reverse. There are, of course, many simpler ways to achieve this result; this is meant as a simple example.
 
 
The script above will run in the command environment of the user.  Because this is a very simple script it will work in all shells that I know of.  You can also use specific notation to instruct your computer which program to use for your script.  See the following example:
 
The script above will run in the command environment of the user.  Because this is a very simple script it will work in all shells that I know of.  You can also use specific notation to instruct your computer which program to use for your script.  See the following example:
 
<pre><nowiki>
 
<pre><nowiki>
第108行: 第75行:
 
</nowiki></pre>
 
</nowiki></pre>
 
This simple script uses two lines. The first informs your system that this script uses the bash program, located at /bin/bash. This is accomplished with the use of '#!'  This is commonly called 'hash-bang'. The second is the command passed to the /bin/bash/ program, in this case 'Hello World' is printed on your terminal.
 
This simple script uses two lines. The first informs your system that this script uses the bash program, located at /bin/bash. This is accomplished with the use of '#!'  This is commonly called 'hash-bang'. The second is the command passed to the /bin/bash/ program, in this case 'Hello World' is printed on your terminal.
 
 
==== Other Scripting Languages ====
 
==== Other Scripting Languages ====
 
 
 
===== Perl =====
 
===== Perl =====
 
 
perl(Practical Extraction and Report Language) [[3 [3]]] is a general purpose scripting language created by Larry Wall. One of Perl's strengths is text manipulation.
 
perl(Practical Extraction and Report Language) [[3 [3]]] is a general purpose scripting language created by Larry Wall. One of Perl's strengths is text manipulation.
 
 
===== Python =====
 
===== Python =====
 
 
Python [[4 [4]]] is a powerful object-oriented scripting language.
 
Python [[4 [4]]] is a powerful object-oriented scripting language.
 
=== Other shells ===
 
=== Other shells ===
 
 
Generally other shells are not recommended; they are mainly there for compatibility with other systems.  For example, you may be developing for an AIX Unix system whose default shell is ''ksh''; thus, you would test under ksh rather than bash.
 
Generally other shells are not recommended; they are mainly there for compatibility with other systems.  For example, you may be developing for an AIX Unix system whose default shell is ''ksh''; thus, you would test under ksh rather than bash.
 
 
====  (pd)ksh ====
 
====  (pd)ksh ====
 
 
''ksh'' [[5 [5]]] or the korn shell is one of the early advanced shells.  Code written in ksh almost always runs in bash without changes, but not the reverse.  It is a common default shell for commercial Unix distributions.
 
''ksh'' [[5 [5]]] or the korn shell is one of the early advanced shells.  Code written in ksh almost always runs in bash without changes, but not the reverse.  It is a common default shell for commercial Unix distributions.
 
 
====  (t)csh ====
 
====  (t)csh ====
 
 
 
The (t)csh [[6 [6]]] scripting language was based around the C syntax and was considered easier to use than the old bourne shell script.  Largely this shell is used to support existing applications written a long time ago. Openoffice.org, for example, uses tcsh shell scripting for historical reasons.
 
The (t)csh [[6 [6]]] scripting language was based around the C syntax and was considered easier to use than the old bourne shell script.  Largely this shell is used to support existing applications written a long time ago. Openoffice.org, for example, uses tcsh shell scripting for historical reasons.
 
 
 
==== zsh ====
 
==== zsh ====
 
 
 
zsh [[7 [7]]] is an advanced scripting language.  It has better command line completion than bash.
 
zsh [[7 [7]]] is an advanced scripting language.  It has better command line completion than bash.
 
 
=== References and Further Reading ===
 
=== References and Further Reading ===
 
 
1. [[Anchor(1)]] [http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html Introduction to Bash Scripting ]<br>
 
1. [[Anchor(1)]] [http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html Introduction to Bash Scripting ]<br>
 
2. [[Anchor(2)]] [http://tldp.org/LDP/abs/html/index.html Advanced Bash Scripting Guide]<br>
 
2. [[Anchor(2)]] [http://tldp.org/LDP/abs/html/index.html Advanced Bash Scripting Guide]<br>

2007年11月30日 (五) 15:16的版本


Introduction

The purpose of this document is to give you an understanding of some of the advanced features of the command line. This document is going to assume you have read the CommandlineHowto. See also the excellent documentation listed in references 1 1 and 2 2.

Command Navigation

Keyboard shortcuts

Here is a list of commonly used keyboard shortcuts using the default shell, bash (/bin/bash): Up Arrow & Down Arrow: previously used commands in the current session.
Ctrl-A: Move cursor to beginning of the current line.
Ctrl-E: Move cursor to the end of the current line.
Ctrl-U: Clear the current line.
Ctrl-H: Same as backspace.
Ctrl-K: Delete all the line from the current cursor position.
Ctrl-W: Delete the word before the current cursor position.
Ctrl-D: On a blank line is the same as the exit command. Otherwise, it deletes the character in front of the cursor.
Ctrl-C: Stop the current running command.
Tab: command completion.

Command history

The history command is a handy tool that allows you to view/use commands that you have already used. One advantage of this tool is when you have to use the same command multiple times. This becomes useful when you a troubleshooting an issue with the system or when you are trying to configure a feature in Linux. Unlike the Up Arrow and Down Arrow keys, as seen in the previous section of this document, history will store all commands used as opposed to the commands used in the current session of terminal. Here is an example of using the history command.

localuser@localhost:~ $ history
1 ls
2 cd /
3 ls
4 history
localuser@localhost:~ $!1
test1.pdf
test2.txt
test3.gif
localuser@localhost:~ $

As you can see in the example above, just the history command will list the last used commands. Also in the example above you are able to execute a previous command by using ! followed by the command number. !! alone will execute the most recent command again.

Miscellaneous Command Line Functions

Here are a couple of miscellaneous functions to help use the command line easier.

Environment Variables

The environment variables are global variables that can be used by all commands. Some of the information stored in the variables is common information as well as system information. An example of an environment variable is PATH, which contains the location of all executables. Using the env command allows you to list as well as modify the environment variables. When you use just the env command alone it will list all the environment variables. Here is an example on how to modify an environment variable:

localuser@localhost:~ $ env HOME=/usr/users/pmd bash

This example will change your home directory before starting a subshell.

alias Command

The alias command allows you to define a new command. Here is an example of using the alias command:

localuser@localhost:~ $ alias l='ls -r'

This example will allow you to use l if you want to see the directory in reverse order.

Scripting

Scripting is the combining of commands into a logical file to complete a specific task. Example of scripts would be a login script that allows you to be able to start programs or maybe setup variables that are used only by that user. Assumed knowledge for this page is that you can use an editor.

Editors

Within the Linux world, much debate has been had on the topic of which is the best editor. Each has their flaws and their advantages, but the only way to determine which is the one for you is to try them, find one you like, and learn how to use it properly. Here is a list of the most commonly used editors. Links have been provided below for additional information about each of the editors.

  • Emacs Emacs is described by the GNU project as "the extensible, customizable, self-documenting real-time display editor." Part of Richard Stallman's GNU project, emacs often suits the newcomer, but has hidden depths which require careful study to release the editor's full capabilities. Note: emacs is not included in a standard Ubuntu installation, and must be installed from the Universe Repository. Emacs Homepage
  • Vim-gnome Vim-gnome is the version of the vim editor provided by Ubuntu. The homepage describes Vim as "an advanced text editor that seeks to provide the power of the de-facto Unix editor 'Vi', with a more complete feature set." Unlike most other editors, Vim is a dual-mode editor, where one mode is used for document editing, while a different mode is used to actually insert text. This can seem daunting to new users, but, just like emacs, the editor has powerful features which need to be learned. Note: Vim-gnome is not installed by default in Ubuntu, but it is available from the Installation CD. [http:www.vim.org/ Vim Homepage]
  • nano The homepage proudly declares that "GNU nano is a small and friendly text editor." Unlike emacs or vim, there is very little to learn about nano, you can type scripts, you can find/replace, you can get started quickly. It does lack some(most) of the features needed by dedicated coders, but it has its own strengths - most especially its tiny size and memory requirements, which mean that it will almost always be installed on any UNIX-based environment. GNU nano is, of course, installed as part of your Ubuntu installation. Nano Homepage

ScriptingHowto

Each time you type on the command line you can save what you are doing and repeat it any number of times by creating a file called a script. You can simply edit a series of repeated commands into a file. Edit a file called reverse and place the following lines in it:

ls -ltr

We now need to change this file to be executable:

$ chmod +x reverse

We can now execute this file:

$ ./reverse

You should see the directory listing in reverse. There are, of course, many simpler ways to achieve this result; this is meant as a simple example. The script above will run in the command environment of the user. Because this is a very simple script it will work in all shells that I know of. You can also use specific notation to instruct your computer which program to use for your script. See the following example:

#!/bin/bash
echo "Hello world"

This simple script uses two lines. The first informs your system that this script uses the bash program, located at /bin/bash. This is accomplished with the use of '#!' This is commonly called 'hash-bang'. The second is the command passed to the /bin/bash/ program, in this case 'Hello World' is printed on your terminal.

Other Scripting Languages

Perl

perl(Practical Extraction and Report Language) [[3 [3]]] is a general purpose scripting language created by Larry Wall. One of Perl's strengths is text manipulation.

Python

Python [[4 [4]]] is a powerful object-oriented scripting language.

Other shells

Generally other shells are not recommended; they are mainly there for compatibility with other systems. For example, you may be developing for an AIX Unix system whose default shell is ksh; thus, you would test under ksh rather than bash.

(pd)ksh

ksh [[5 [5]]] or the korn shell is one of the early advanced shells. Code written in ksh almost always runs in bash without changes, but not the reverse. It is a common default shell for commercial Unix distributions.

(t)csh

The (t)csh [[6 [6]]] scripting language was based around the C syntax and was considered easier to use than the old bourne shell script. Largely this shell is used to support existing applications written a long time ago. Openoffice.org, for example, uses tcsh shell scripting for historical reasons.

zsh

zsh [[7 [7]]] is an advanced scripting language. It has better command line completion than bash.

References and Further Reading

1. Anchor(1) Introduction to Bash Scripting
2. Anchor(2) Advanced Bash Scripting Guide
3. Anchor(3) Learning perl
4. Anchor(4) Python
5. Anchor(5) (pd)ksh
6. Anchor(6) (t)csh
7. Anchor(7) zsh