码迷,mamicode.com
首页 > 其他好文 > 详细

Command Line Skills

时间:2015-09-13 21:17:35      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

Part 1: Command Line Interface(CLI)

The Command Line Interface (CLI), is a text-based interface to the computer, where the user types in a command and the computer then executes it. The CLI environment is provided by an application on the computer known as a terminal.

The terminal accepts what the user types and passes to a shell. The shell interprets what the user has typed into instructions that can be executed by the operating system. If output is produced by the command, then this text is displayed in the terminal. If problems with the command are encountered, then an error message is displayed.

 

1. Prompt

A terminal window displays a prompt; the prompt appears when no commands are being run and when all command output has been printed to the screen. The prompt is designed to tell the user to enter a command.

The structure of the prompt may vary between distributions, but will typically contain information about the user and the system. Below is a common prompt structure:

[sysadmin@localhost ~]

 

The previous prompt provides the name of the user that is logged in (sysadmin), the name of the system (localhost) and the current directory (~). The ~ symbol is used as shorthand for the user‘s home directory (typically the home directory for the user is under the /home directory and named after the user account name, for example: /home/sysadmin).

 

2. Shell

A shell is the interpreter that translates commands entered by a user into actions to be performed by the operating system. The Linux environment provides many different types of shells, some of which have been around for many years.

The most commonly used shell for Linux distributions is called the BASH shell. It is a shell that provides many advanced features, such as command history, which allows you to easily re-execute previously executed commands.

The BASH shell also has other popular features:

  • Scripting: The ability to place commands in a file and execute the file, resulting in all of the commands being executed. This feature also has some programming features, such as conditional statements and the ability to create functions (AKA, subroutines).
  • Aliases: The ability to create short "nicknames" for longer commands.
  • Variables: Variables are used to store information for the BASH shell. These variables can be used to modify how commands and features work as well as provide vital system information.

Note: The previous list is just a short summary of some of the many features provided by the BASH shell.

 

3. Formatting Commands

Many commands can be used by themselves with no further input. Some commands require additional input to run properly. This additional input comes in two forms: options and arguments.

The typical format for a command is as follows:

command [options] [arguments]

 

Options are used to modify the core behavior of a command while arguments are used to provide additional information (such as a filename or a username). Each option and argument is normally separated by a space, although options can often be combined together.

Keep in mind that Linux is case senstive. Commands, options, arguments, variables and filenames must be entered exactly as shown.

 

The ls command will provide useful examples. By itself, the ls command will list the files and directories contained in your current working directory:

技术分享
The ls command will be covered in complete detail in a later chapter. The purpose of introducing this command now is to demonstrate how arguments and options work. At this point you shouldn‘t worry about what the output of the command is, but rather focus on understanding what an argument and an option is.

An argument can also be passed to the ls command to specify which directory to list the contents of. For example, the command ls /etc/ppp will list the contents of the /etc/ppp directory instead of the current directory:

技术分享

Since the ls command will accept multiple arguments, you can list the contents of multiple directories at once by typing the ls /etc/ppp /etc/sshcommand:

技术分享

 

4. Working with Options

Options can be used with commands to expand or modify the way a command behaves. Options are often single letters; however, they sometimes will be "words" as well. Typically, older commands use single letters while newer commands use complete words for options. Single-letter options are preceded by a single dash (-). Full-word options are preceded by two dashes (--).

For example, you can use the -l option with the ls command to display more information about the files that are listed. The ls -l command will list the files contained within the current directory and provide additional information, such as the permissions, the size of the file and other information:

技术分享

In most cases, options can be used in conjunction with other options. For example, the ls -l -h or ls -lh command will list files with details, but will display the file sizes in human readable format instead of the default value (bytes):

技术分享

Note that the previous example also demonstrated how you can combine single letter options: -lh. The order of the combined options isn‘t important.

The -h option also has a full-word form: --human-readable.

Options can often be used with an argument. In fact, some options require their own arguments. You can use options and arguments with the ls command to list the contents of another directory by executing the ls -l /etc/pppcommand:

技术分享

 

5. Command history

When you execute a command in a terminal, the command is stored in a "history list". This is designed to make it easy for you to execute the same command later since you won‘t need to retype the entire command.

To view the history list of a terminal, use the history command:

技术分享

Pressing the Up Arrow key will display the previous command on your prompt line. You can press up repeatedly to move back through the history of commands you have run. Pressing the Enter key will run the displayed command again.

When you find the command that you want to execute, you can use the Left arrow keys and Right arrow keys to position the cursor for editing. Other useful keys for editing include the Home, End, Backspace and Delete keys.

If you see a command you wish to run in the list that the history command generates, you can execute this command by typing an exclamation point and then the number next to the command, for example:

!3

 

技术分享

Some additional history examples:

Example

Meaning

history 5 Show the last five commands from the history list
!! Execute the last command again
!-5 Execute the fifth command from the bottom of the history list
!ls Execute the most recent ls command

 

6. Introducing BASH shell variables

A BASH shell variable is a feature that allows you or the shell to store data. This data can be used to provide critical system information or to change the behavior of how the BASH shell (or other commands) work.

Variables are given names and stored temporarily in memory. When you close a terminal window or shell, all of the variables are lost. However, the system automatically recreates many of these variables when a new shell is opened.

To display the value of a variable, you can use the echo command. The echocommand is used to display output in the terminal; in the example below, the command will display the value of the HISTSIZE variable:

技术分享

The HISTSIZE variable defines how many previous commands to store in the history list. To display the value of the variable, use a dollar sign $ character before the variable name. To modify the value of the variable, you don‘t use the $ character:

技术分享
There are many shell variables that are available for the BASH shell, as well as variables that will affect different Linux commands. A discussion of all shell variables is beyond the scope of this chapter, however more shell variables will be covered as this course progresses.

 

7. PATH variable

One of the most important BASH shell variables to understand is the PATHvariable.

The term path refers to a list that defines which directories the shell will look in for commands. If you type in a command and receive a “command not found” error, it is because the BASH shell was unable to locate a command by that name in any of the directories included in the path. The following command displays the path of the current shell:

技术分享

Based on the proceeding output, when you attempt to execute a command, the shell will first look for the command in the /usr/lib/qt-3.3/bin directory. If the command is found in that directory, then it is executed. If it isn‘t found, then the shell will look in the /usr/local/bin directory.

If the command is not found in any directory listed in the PATH variable, then you will receive a "command not found" error:

技术分享

If custom software is installed on your system, you may need to modify thePATH to make it easier to execute these commands. For example, the following will add the /usr/bin/custom directory to the PATH variable:

技术分享

 

8. export Command

There are two types of variables used in the BASH shell, local and environment. Environment variables, such as PATH and HOME, are used by BASH when interpreting commands and performing tasks. Local variables are often associated with user based tasks and are lowercase by convention. To create a local variable, simply type:

sysadmin@localhost:~$variable1=Something

 

To view the contents of the variable, refer to it with a leading $ sign:

sysadmin@localhost~:$echo $variable
Something

To view environment variables, use the env command (searching through the output using grep, as shown here, will be discussed in later chapters). In this case, the search for variable1 in the environment variables results in no output:

sysadmin@localhsot:~$env | grep variable1

After exporting variable1, it is now an environment variable. Notice that this time, it is found in the search through the environment variables:

sysadmin@localhost:~$export variable1
sysadmin@localhost:~$env | grep variable1
variable1=Something

The export command can also be used to make an environment variable upon its creation:

sysadmin@localhost:~$export variable2=‘Else‘
sysadmin@localhost:~$env | grep variable2
variable1=Else

To change the value of an environment variable, simply omit the $ when referencing it:

sysadmin@localhost:~$ variable1=$variable1   $variable2
sysadmin@localhost:~$ echo $variable1
Something Else

 

Exported variables can be removed using the unset command:

sysadmin@localhost:~$ unset $variable2

 

9. which Command

There may be situations where different versions of the same command are installed on a system or where commands are accessible to some users and not others. If a command does not behave as expected or if a command is not accessible that should be, it can be beneficial to know where the shell is finding the command or which version it is using.

It would be tedious to have to manually look in each directory that is listed in thePATH variable. Instead, you can use the which command to display the full path to the command in question:

技术分享

技术分享

技术分享
$which data
/bin/date
$which cal
/usr/bin/cal

 

The which command searches for the location of a command by searching thePATH variable.

 

10. type Command

The type command can be used to determine information about various commands. Some commands originate from a specific file:

$type which
which is /usr/bin/which

This output would be similar to the output of the which command (as discussed in the previous section, which displays the full path of the command):

$which which
/usr/bin/which

The type command can also identify commands that are built into the bash (or other) shell:

$type echo
echo is a shell builtin

In this case, the output is significantly different from the output of the whichcommand:

$which echo
/usr/bin/which

Using the -a option, the type command can also reveal the path of another command:

$type -a echo
echo is a shell builtin
ehco is /bin/echo

The type command can also identify aliases to other commands:

$type ll
ll is aliased to ls -alF
$type ls
ls is aliased to ls --color=auto

The output of these commands indicate that ll is an alias for ls -alF, and even ls is an alias for ls --color=auto. Again, the output is significantly different from the which command:

$which ll
$which ls
/bin/ls

The type command supports other options, and can lookup multiple commands simultaneously. To display only a single word describing the echo,ll, and which commands, use the -t option:

$type -t echo ll which
builtin
alise
file

 

11. Aliases

An alias can be used to map longer commands to shorter key sequences. When the shell sees an alias being executed, it substitutes the longer sequence before proceeding to interpret commands.

For example, the command ls -l is commonly aliased to l or ll. Because these smaller commands are easier to type, it becomes faster to run the ls -lcommand line.

You can determine what aliases are set on your shell with the alias command:

技术分享

The aliases that you see from the previous examples were created by initialization files. These files are designed to make the process of creating aliases automatic and will be discussed in more detail in a later chapter.

New aliases can be created by typing alias name=command where name is the name you want to give the alias and command is the command you want to have executed when you run the alias.

For example, you can create an alias so that lh displays a long listing of files, sorted by size with a “human friendly” size with the alias lh=‘ls -Shl‘command. Typing lh should now result in the same output as typing the ls -Shl command:

技术分享

Aliases created this way will only persist while the shell is open. Once the shell is closed, the new aliases that you created will be lost. Additionally, each shell has its own aliases, so if you create an alias in one shell and then open another shell, you won‘t see the alias in the new shell.

 

12. Globbing

Glob characters are often referred to as “wild cards”. These are symbols that have special meaning to the shell.

Unlike commands that the shell will run, or options and arguments that the shell will pass to commands, glob characters are interpreted by the shell itself before it attempts to run any command. This means that glob characters can be used with any command.

Globs are powerful because they allow you to specify patterns that match filenames in a directory, so instead of manipulating a single file at a time, you can easily execute commands that will affect many files. For instance, by using glob characters it is possible to manipulate all files with a certain extension or with a particular filename length.

Keep in mind that these globs can be used with any command because it is the shell, not the command that expands with globs into matching filenames. The examples provided in this chapter will use the echo command for demonstration.

 

Asterisk(*)

The asterisk character is used to represent zero or more of any character in a filename. For example, suppose you want to display all of the files in the /etc directory that begin with the letter "t":

技术分享

The pattern "t*" means "match any file that begins with the character t and has zero or more of any character after the t".

You can use the asterisk character at any place within the filename pattern. For example, the following will match any filename in the /etc directory that ends with ".d":

技术分享

In the next example, all of the files in the /etc directory that begin with the letter "r" and end with ".conf" will be displayed:

技术分享

 

Question Mark(?)

The question mark represents any one character. Each question mark character matches exactly one character, no more and no less.

Suppose you want to display all of the files in the /etc directory that begin with the letter "t" and have exactly 7 characters after the "t" character:

技术分享

Glob characters can be used together to find even more complex patterns. Theecho /etc/*???????????????????? command will print only files in the/etc directory with twenty or more characters in the filename:

技术分享

The asterisk and question mark could also be used together to look for files with three-letter extensions by running the echo /etc/*.??? command:

技术分享

 

Brackets []

Brackets are used to match a single character by representing a range of characters that are possible match characters. For example, echo /etc/[gu]* will print any file that begins with either a “g” or “u” character and contains zero or more additional characters:

技术分享

Brackets can also be used to a represent a range of characters. For example, the echo /etc/[a-d]* command will print all files that begin with any letter between and including “a” and “d”:

技术分享

The echo /etc/*[0-9]* command would display any file that contains at least one number:

技术分享

The range is based on the ASCII text table. This table defines a list of characters, arranging them in a specific standard order. If you provide an invalid order, no match will be made:

技术分享

 

Exclamation Point (!)

The exclamation point is used in conjunction with the square brackets to negate a range. For example, the command echo [!DP]* will display any file thatdoes not begin with a “D” or “P”.

 

13. Quoting

There are three types of quotes that have special significance to the Bash shell: double quotes ("), single quotes (‘), and back quotes (`). Each set of quotes indicates to the shell that it should treat the text within the quotes differently than it would normally be treated.

 

Double Quotes

Double quotes will stop the shell from interpreting some metacharacters, including glob characters. Within double quotes an asterisk is just an asterisk, a question mark is just a question mark, and so on. This means that when you use the second echo command below, the BASH shell doesn‘t convert the glob pattern into filenames that match the pattern:

技术分享

This is useful when you want to display something on the screen that is normally a special character to the shell:

技术分享

Double quotes still allow for command substitution (discussed later in this chapter), variable substitution and permit some other shell metacharacters that haven‘t been discussed yet. For example, in the following demonstration, you will notice that the value of the PATH variable is displayed:

技术分享

 

Single Quotes

Single quotes prevent the shell from doing any interpreting of special characters. This includes globs, variables, command substitution and other metacharacter that have not been discussed yet.

For example, if you want the $ character to simply mean a $, rather than it acting as an indicator to the shell to look for the value of a variable, you could execute the second command displayed below:

技术分享
 

Backslash Chracter(\)

You can use an alternative technique to essentially single quote a single character. For example, suppose you want to print the following: "The services costs $100 and the path is $PATH". If you place this in double quotes, $1 and $PATH are considered variables. If you place this in single quotes, $1 and $PATH are not variables. But what if you want to have $PATH treated as a variable and $1 not?

If you place a backslash ( \ ) character in front of another character, it treats the other character as a "single quoted" character. The third command below demonstrates using the \ character while the other two demonstrate how the variables would be treated within double and single quotes:

技术分享

 

Back Quotes

Back quotes are used to specify a command within a command, a process called command substitution. This allows for very powerful and sophisticated use of commands.

While it may sound confusing, an example should make things more clear. To begin, note the output of the date command:

技术分享

Now note the output of the echo Today is date command line:

技术分享

In the previous command the word “date” is treated as regular text and the shell simply passes "date" to the echo command. But, you probably want to execute the date command and have the output of that command sent to the echocommand. To accomplish this, you would run the echo Today is `date`command line:

技术分享

 

14 Control Statements

Control statements allow you to use multiple commands at once or run additional commands, depending on the success of a previous command. Typically these control statements are used within scripts, but they can also be used on the command line as well.

 

Semicolon

The semicolon can be used to run multiple commands, one after the other. Each command runs independently and consecutively; no matter the result of the first command, the second will run once the first has completed, then the third and so on.

For example, if you want to print the months of January, February and March of 2014, you can execute cal 1 2014; cal 2 2014; cal 3 2014 on the command line:

技术分享

 

 

Double Ampersand (&&)

The double ampersand ( && ) acts as a logical “and”; if the first command is successful, then the second command (to the right of &&) will also run. If the first command fails, then the second command will not run.

To better understand how this works, consider first the concept of failure and success for commands. Commands succeed when they work properly and fail when something goes wrong. For example, consider the ls /etc/xmlcommand line. The command will succeed if the /etc/xml directory is accessible and fail if it isn‘t.

For example, the first command will succeed because the /etc/xml directory exists and is accessible while the second command will fail because there is no/junk directory:

技术分享

The way that you would use the success or failure of the ls command in conjunction with && would be to execute a command line like the following:

技术分享

In the first example above, the echo command executed because the ls command succeeded. In the second example, the echo command wasn‘t executed because the ls command failed.

 

Double Pipe

The double pipe ( || ) is a logical “or”. It works in a similar way to the double ampersand; depending on the result of the first command, the second command will either run or be skipped.

With the double pipe, if the first command runs successfully, the second command is skipped; if the first command fails, then the second command will be run. In other words, you are essentially telling the shell, “Either run this first command or the second one”.

In the following example, the echo command will only execute if the lscommand fails:

技术分享

 

Command Line Skills

标签:

原文地址:http://www.cnblogs.com/elewei/p/4805454.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!