Use of shell in Centos

Source: Internet
Author: User

What is shell?
The intention of shell is "shell". In fact, it has clearly explained the role of shell in Linux. Shell is a "shell" program outside the Linux kernel. All tasks completed by the user on the operating system are implemented through the interaction between shell and the Linux kernel. We should be familiar with the command.com program in the DOS system. shell functions are similar, but shell functions are more powerful and easier to use.

Various operating systems have their own shells. Take DOS as an example. Its shell is a command.com program. There are also many third-party command interpreter programs in DOS, such as 4DOS and NDOS. These command interpreter programs can completely replace the standard command.com program. In Linux, apart from the default Bourne again shell (bash), there are many other shells, such as C shell (csh), Korn shell (ksh), And Bourne shell (sh) and Tenex C shell (tcsh. The shell functions of each version are basically the same, but each has its own merits. The current Linux system release usually uses bash as the default shell.

Shell is a C language program and a bridge between users and the operating system kernel. Shell is both a command interpreter and a powerful interpreted programming language. As a command interpreter, shell interprets the command entered by the user, submits it to the kernel for processing, and finally returns the result to the user.

To speed up the running of commands and more effectively customize shell programs, shell defines some built-in commands. Generally, the commands interpreted by shell itself are called built-in commands, for example, the cd, pwd, exit, and echo commands we will talk about below are all built-in bash commands. After a user logs on to the system, the shell and built-in commands are loaded into the memory and run until the user exits the system. In addition to built-in commands, there are many executable files in Linux. Executable files are stored in .exe files in windows. These executable files can also be executed as shell commands. In fact, many commands in Linux are not built-in shell commands. For example, ls is an executable file, which is stored in/bin/ls. These commands are different from the built-in shell commands. The system loads these commands into the memory for execution only when they are called.

After a user logs on to the system, a shell command prompt appears on the logon character page. "#" Indicates that the login user is a system superuser, and "*" indicates that the login user is a common user. The specific process of shell command execution is as follows: After you input the command to submit the command on the command line, the shell program first checks whether the command is a built-in command. If yes, the shell interpreter interprets the command as a system call and then submits it to the kernel for execution. If it is not a built-in shell command, shell searches for the corresponding commands on the hard disk based on the path provided by the user or the configuration information of the system environment variables, transfers the commands to the memory, and finally interprets them as system calls, submit to the kernel for execution.

Shell is also a powerful interpreted programming language. It defines various options and variables and supports almost all program structures in advanced programming languages, such as variables, functions, expressions, and loops. You can use shell to write shell scripts, which are similar to batch files in Windows/DOS. However, shell functions are more complete and powerful.

4.1.2 syntax analysis of shell commands
The main difference between various shells in Linux is the command line syntax. For some common commands, the syntax of each shell version is basically the same. Only when a shell script is compiled or some advanced shell features are used, the differences between shell versions are displayed.

Shell syntax analysis refers to the shell's scanning and processing of commands, that is, to break down commands or user input into operations to process each part. In Linux, shell syntax analysis contains a lot of content, such as redirection, file name extension, and pipelines.

This section uses bash as an example to describe the syntax analysis of the following shell commands.

1. shell Command Format
After the user logs on to the system, the shell command line starts. Shell parses and interprets the commands entered by the user according to a certain syntax format and passes them to the system kernel. The general format of shell commands is:

Command [options] [arguments]

According to our habits, we generally refer to strings in the above format as command lines. The command line is the basic unit of the dialog between the user and the shell.

-Command: indicates the command name.

-Options: indicates the Command Option.

-Arguments: indicates the command parameters.

In a command line, an option is a code that contains one or more letters. It is mainly used to change the command execution mode. Generally, there is a "-" symbol before the option to differentiate parameters. For example:

[Root @ WEBServer ~] # Ls-

Add the-a option to the ls command to list all files (including hidden files) in the current directory ). If "-a" is not added to ls, only the file names and directories under the current directory are displayed (hidden files are not displayed ).

Generally, commands have many options, which can be listed separately or listed after "-". For example,

Ls-a-l

You can also write it as follows:

Ls-al

Parameters are acceptable for many commands. A parameter is one or more strings followed by the option. These strings specify the operation object of the command, such as a file or directory. For example, to display all files and information in the/etc directory, run the following command:

[Root @ WEBServer ~] # Ls-al/etc

In special cases, some commands do not include parameters, such as ls commands, but some must contain parameters. If the parameter is not enough, the shell will prompt an error. For example, the mv Command requires at least two parameters.

[Root @ WEBServer ~] # Mv mylinux1.txt mylinux.txt

In a shell command line, you can also enter multiple commands and separate them with semicolons. For example:

[Root @ WEBServer ~] # Ls-al; cp mylinux1.txt mylinux2.txt

On the contrary, you can enter a command in multiple lines and use "\" to continue a command to the next line:

[Root @ WEBServer ~] # Cp-I \

> Mylinux1.txt \

> Mylinux2.txt

2. shell wildcard
Wildcard characters are used to facilitate the description of a file or directory. For example, if you only need a file ending with ". sh", wildcard characters can be used to conveniently implement it. All versions of shell have wildcards, which are special characters. You can use these characters in command line parameters to match the file name or path name. Shell uses all the file names or path names that match the rule specified in the command line as command parameters and then runs the command.

Common wildcards in bash include "*" and "?". And "[]".

(1) "*" -- match any one or more characters

For example:

[Root @ WEBServer ~] # Ls *. txt

This command is used to list all files ending with "." in the current directory (except for files starting ).

[Root @ WEBServer ~] # Cp doc/*/opt

Copy all files in the doc directory (excluding files starting with ".") to the/opt directory.

[Root @ WEBServer ~] # Ls-al/etc/*. conf

List all files ending with ". conf" under the/etc directory. Files ending with ". conf" in the/etc directory will not be listed.

(2) "?" -- Match any single character

For example:

[Root @ WEBServer ~] # Ls AB ?. Txt

Starting with a forward slash (A), and connecting a file ending with a forward slash.

[Root @ WEBServer ~] # Ls AB ??. Txt

Keep the contents listed in the predicate directory as a lead, and the two keys are arbitrary, and the files ended with a slash.

(3) "[]" -- match any single character contained in square brackets

For example:

[Root @ WEBServer ~] # Ls/dev/sda [12345]

/Dev/sda1/dev/sda2/dev/sda3/dev/sda4/dev/sda5

The above list all files starting with sda in the/dev directory, with 4th characters being 1, 2, 3, 4, or 5.

[Root @ WEBServer ~] # Ls/dev/sda [1-5]

In square brackets, "1-5" gives the matching range, which is exactly the same as the preceding command.

(4) wildcard combination

In Linux, wildcards can also be used in combination, for example:

[Root @ WEBServer ~] # Ls [0-9]?. Conf

List all files starting with a number in the current directory, followed by any character ending with ". conf.

[Root @ WEBServer ~] # Ls [xyz] *. txt

List all objects starting with xpolicyor zand ending with "cmd.txt" in the current directory.

3. shell redirection
In Linux, three files are opened: standard input, standard output, and standard error output. The user's shell sets the keyboard as the default standard input, and the default standard output and standard error output as the screen. That is, the user enters the command from the keyboard and outputs the result and error information to the screen.

The so-called redirection means that the system does not use the default standard input and output, but is re-specified. Therefore, redirection can be divided into input redirection, output redirection, and error output redirection. To implement redirection, you need to understand the redirection operator. shell decides the redirection operation based on the redirection operator.

(1) input redirection

Input redirection is used to change the input source of a command. With the input redirection, you can use the content of a file as the command input without entering it on the keyboard.

The operators used for input redirection include "<" and "<". For example:

[Root @ WEBServer ~] # Wc </etc/inittab

53 229 1666

Use the wc command to count the number of lines, words, and characters of the/etc/inittab file.

Another type of input redirection is "<", which tells shell that the standard input of the current command is the content from a pair of separators in the command line. For example:

[Root @ WEBServer ~] # Wc <aa

> # Default runlevel. The runlevels used by RHS are:

> #0-halt (Do NOT set initdefault to this)

> #1-Single user mode

> #2-Multiuser, without NFS (The same as 3, if you do not have networking)

> #3-Full multiuser mode

> #4-unused

> #5-X11

> #6-reboot (Do NOT set initdefault to this)

> Aa

8 65 303

The preceding command uses the content of a pair of separator aa as the wc command input. The separator can be any character. Shell reads the content after the first separator until another separator is read and then sends the content to the wc command for processing.

(2) Output redirection

The output redirection is to output the command output result to a specified file instead of the screen output.

In Linux, the output redirection is used a lot. For example, the output of a command is very long and the screen cannot be displayed. We can specify the output of the command to a file, run the more command to view the file and obtain the complete command output information.

The operators used for output redirection include ">" and "> ". For example:

[Root @ WEBServer ~] # Ps-ef> ps.txt

The system running process information of the ps-ef system is input to the ps.txt file without being output to the screen. You can use the morecommand to view the process information of the system running in the ps.txt file.

[Root @ WEBServer ~] # More file1 file2 file3> file

The more command is to view the content of the file. The above command is to output all the content of file1, file2, and file3 to the file, similar to the merging of the file content.

If the specified file after ">" does not exist, shell will automatically recreate it. If the file exists, the original content of the file will be overwritten; if you do not want to overwrite an existing file, you can use the ">" operator. For example:

[Root @ WEBServer ~] # Ls-al/etc/*>/root/install. log

Append all the file information in the/etc directory and Its subdirectories to the end of the/root/install. log file. The original content of the/root/install. log File still exists.

(3) Error redirection

Error redirection is the same as standard output redirection. You can use the operators "2>" and "2>" to redirect error output. For example:

[Root @ WEBServer ~] # Tar zxvf text.tar.gz 2> error.txt

Tar is a packaging command. You can see the tar decompression process on the screen. If text.tar.gz.pdf is a bad compressed package, the error information will be output to the error.txt file.

4. shell MPs queue
The pipeline can connect many commands. It can regard the input of 1st commands as the output of 2nd commands, and the output of 2nd commands as the input of 3rd commands, and so on. Therefore, the role of a pipeline is to treat the output of a command as the input of the next command without passing through any intermediate file.

You can use the pipe character "|" to establish a pipe connection, for example:

[Root @ WEBServer ~] # Ls-al/etc/* | more

Displays all files in the/etc directory and sub-directories on a screen.

[Root @ WEBServer ~] # Ps-ef | grep httpd | wc-l

This command is used to view httpd processes running in the system and calculate the number of httpd processes.

5. References in shell
Bash has many special characters which have special meanings. If you use them in shell parameters, problems may occur. In Linux, the "Reference" technology is used to ignore the special meanings of these characters. The reference technology is to notify shell to treat these special characters as common characters. The Characters Used for referencing in shell include the Escape Character "\", single quotation mark "'", and double quotation mark ".

(1) Escape Character "\"

If "\" is placed before a special character, shell ignores the original meaning of these special characters and treats them as common characters. For example:

[Root @ WEBServer ~] # Ls

Abc? * C: \ backup

[Root @ WEBServer ~] # Mv abc \? \ * Abc

[Root @ WEBServer ~] # Mv C \: \ backup

In the above example? * Re-command abc to rename C: \ backup to backup. Because the file name contains special characters, all use the Escape Character "\".

(2) single quotation marks (')

Put the string between one pair of single quotes, the special meaning of all characters in the string will be ignored, for example:

[Root @ WEBServer ~] # Mv C \: \ backup

[Root @ WEBServer ~] # Mv 'C: \ backup 'backup

The preceding two commands are completely equivalent.

(3) double quotation marks"

Double quotation marks are similar to single quotation marks. Most special characters contained in double quotation marks can be treated as common characters, but some special characters are enclosed in double quotation marks, it also retains its own special meanings, such as "$", "\", and "'".

[Root @ WEBServer ~] # Str = "The \ $ SHELL Current shell is $ SHELL"

[Root @ WEBServer ~] # Str1 = "\ $ SHELL"

[Root @ WEBServer ~] # Echo $ str

The $ SHELL Current shell is/bin/bash

[Root @ WEBServer ~] # Echo $ str1

$/Bin/bash

The output above shows that "$" and "\" still have special meanings in double quotation marks.

[Root @ WEBServer ~] # Str = "This hostname is 'hostname '"

[Root @ WEBServer ~] # Echo $ str

This hostname is WEBServer

In the above output, the character "'" also retains its special meaning in double quotation marks.

6. shell auto-completion command line
Automatic completion of command lines is a simple and practical function of bash. Automatic completion of command lines means that you do not have to enter all commands when entering commands. shell can intelligently judge the commands you want to input.

After a user enters a part of a command, press the "Tab" key, and shell will prompt all commands and files similar to the user's input command based on the system environment variable information, such:

[Root @ WEBServer ~] # If <press the Tab key>

If ifcfg ifconfig ifdown ifenslave ifnames ifrename ifup

[Root @ WEBServer ~] # If

As you can see from the above, you can enter "if" and press the "Tab" key to display all commands and files prefixed with "if. If you need the ifconfig command, you only need to enter "co" again and press the "Tab" key to complete the command.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.