What is Shell,shell basics, common shell commands, usage, tricks

Source: Internet
Author: User
Tags echo command stdin

Shell: Can batch processing, automation to complete a series of maintenance tasks, greatly reduce the burden of administrators! The shell is a special application that acts as a "command interpreter" between the operating system kernel and the user. is responsible for receiving the user input of the operation instructions, and explain, will need to perform the operation passed to the kernel execution, and output execution results.

How to view the shell supported by the system (default is bash):

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image002 "border=" 0 "alt=" clip_ image002 "src=" http://s3.51cto.com/wyfs02/M01/75/15/wKioL1YyOLSRmdttAABMQOQFjt4812.jpg "height=" 126 "/>

Shell script format: composed of script declaration, script comment, executable statement:
650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image004 "border=" 0 "alt=" clip_ image004 "src=" http://s3.51cto.com/wyfs02/M01/75/15/wKioL1YyOLeSUc4-AABleG0dA6k987.jpg "height=" "/>"

Shell script execution: Simply save the various Linux commands you normally use in order to a text file, and then add executable permissions, this file becomes a shell script! Ps:chmod +x target file

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image006 "border=" 0 "alt=" clip_ image006 "src=" http://s3.51cto.com/wyfs02/M00/75/18/wKiom1YyOIGhNihjAAFG79nCLOM776.jpg "height=" 268 "/>
REDIRECT operation with pipe symbol:

1. REDIRECT operation: In the Linux system, all hardware resources have been file mode in the system logic display, such as: hard disk and partition, CD-ROM and other devices. The following interactive device files are included.

A: Standard input (STDIN): The default device is the keyboard, the file number is 0, and the command reads the input data that is required during execution from the standard input file.

B: Standard Output (STDOUT): The default device is the display, the file number is 1, the command will be executed after the output sent to the standard output file.

C: Standard error (STDERR): The default device is the monitor, the file number is 2, and the command sends various error messages during execution to the specified error file.

1): Redirect output: Refers to saving the normal output of the command to the specified file, rather than directly on the display screen. Use the ">", ">>" symbol to indicate that the former is the meaning of preservation, the latter is appended.

For example, save the current host's CPU type information (UNAME-P) to the Cpuinfo.txt file.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image008 "border=" 0 "alt=" clip_ image008 "src=" http://s3.51cto.com/wyfs02/M01/75/18/wKiom1YyOILg4sYtAABalm5X6RY536.jpg "height="/>

For example, when you need to save the original contents of the destination file, you should change the ">>" action symbol to append the content instead of overwriting the file.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image010 "border=" 0 "alt=" clip_ image010 "src=" http://s3.51cto.com/wyfs02/M02/75/18/wKiom1YyOIKDvXxzAABWLHUNm8w864.jpg "height=" "/>

2): Redirect input: Refers to the way the command receives input from the default keyboard to the specified file acquisition, and redirects the input using the "<" operator.

For example, by redirecting the input, some interactive operations can be done by reading the file. This can greatly reduce the process of program interruption and improve the efficiency of script execution. (The--stdin option of the passwd command is used to identify the standard input)

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image012 "border=" 0 "alt=" clip_ image012 "src=" http://s3.51cto.com/wyfs02/M00/75/18/wKiom1YyOIOwMvSdAAEb6kcdzos258.jpg "height=" 199 "/>

3): Error redirection: Refers to the error message that occurs during the execution of the command, saved to the specified file, and not displayed on the screen. Error redirection uses the "2>" operator, where "2" refers to the number of the error file. (The standard input, output omitted 0 and 1)

For example, you can save the error message that occurs when you back up using the tar command to a error.log file.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image014 "border=" 0 "alt=" clip_ image014 "src=" http://s3.51cto.com/wyfs02/M01/75/18/wKiom1YyOIOCILGVAABzrkWt4XU871.jpg "height=" Bayi "/>

PS: Using the "2>" action symbol, you will overwrite the contents of the target file as with ">", use "2>>" if you want to append the content, or use ">", "2>" to save two types of output information to separate files, or you can use the &> "Save to the same file!"

2. Pipe symbols: Pipe symbols provide a mechanism for collaboration between different commands, located in the pipe symbol "|" The command output on the left will be the input object for the right command, and multiple pipelines can be used in the same line command.

For example, when using the grep command to query a user name using "/bin/bash" as the shell, the entire line of content that matches the condition is output, which can be further filtered with the awk command in conjunction with the pipeline operation, outputting only the user name and login shell.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image016 "border=" 0 "alt=" clip_ image016 "src=" http://s3.51cto.com/wyfs02/M02/75/18/wKiom1YyOIPCEQG0AACLKbNIM6w728.jpg "height=" 119 "/>

PS: In the preceding example, the awk function is to output a string of the first and seventh regions with the colon ":" as the delimiter. Where the "-F" section is used to specify the separator symbol (any symbol can be specified)

Use shell variables:

1. Customize the new variable: format "variable name = variable Value"

Custom variables are defined by the system user and are only valid in the user's own shell environment, also known as local variables (variable names start with a letter or underscore, are case-sensitive, are recommended all uppercase, and cannot contain special symbols such as: +-*/?). % #, etc.).

For example:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image018 "border=" 0 "alt=" clip_ image018 "src=" http://s3.51cto.com/wyfs02/M02/75/15/wKioL1YyOLnCn7xQAAAwqMrtQ-4738.jpg "height=" "/>"

2. Viewing and referencing the value of a variable

You can refer to the value of a variable by adding the leading symbol "¥" before the variable name. With the echo command, you can view multiple variable values at the same time, with no spaces in between.

Ps:echo–e option: Used to call special characters, support backslash control character conversion!

For example:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image020 "border=" 0 "alt=" clip_ image020 "src=" http://s3.51cto.com/wyfs02/M00/75/16/wKioL1YyOLnQsLlKAABQgVCP8sE105.jpg "height=" "/>

When referencing variables and outputting new content that you add yourself, you should enclose the variable names in the "{}" notation.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image022 "border=" 0 "alt=" clip_ image022 "src=" http://s3.51cto.com/wyfs02/M01/75/16/wKioL1YyOLnB4t3nAABAq_fVLZo715.jpg "height="/>

3. Special actions for assigning variables

A: Double quotation marks (""): the function of defining a string, especially when the content to be assigned contains spaces, must be enclosed in double quotation marks, and generally omitted. (PS: Double quotation marks are mainly used to call other variable values, or use "white space", plus double quotation marks, other cases can be omitted)

For example: assigning "Benet 5.0" to a variable benet should perform a "benet=" Benet 5.0 "operation.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image024 "border=" 0 "alt=" clip_ image024 "src=" http://s3.51cto.com/wyfs02/M02/75/16/wKioL1YyOLqxm21pAABgeEVH-ds095.jpg "height="/>

Within the double quotation mark range, use the "$" symbol to refer to the values of other variables (variable references)

For example:
650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image026 "border=" 0 "alt=" clip_ image026 "src=" http://s3.51cto.com/wyfs02/M00/75/18/wKiom1YyOIThiUrtAABCzhtfirc183.jpg "height=" "/>"

B: Single quotes ('): When you need to include special symbols $ "\ Characters in the contents of the assignment, you should use single quotation marks around them, and the characters within their range are treated as ordinary symbols!

However, when you include single quotes in an assignment, you can use the "\" symbol to escape the conflict. (PS: You can also omit the single quotation mark, the special characters with the "\" symbol to escape)

For example:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image028 "border=" 0 "alt=" clip_ image028 "src=" http://s3.51cto.com/wyfs02/M01/75/18/wKiom1YyOITj4gyjAABJcyZNw6o598.jpg "height=" "/>"

C: Anti-apostrophe ('): This key is below the ESC key.

The anti-apostrophe master is used for command substitution, which allows the execution result of the command to be output as the value of the variable.

For example, to find the location of the Useradd command program in one line of commands and to list the details:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image030 "border=" 0 "alt=" clip_ image030 "src=" http://s3.51cto.com/wyfs02/M02/75/18/wKiom1YyOITirLmoAAA_eofZusA766.jpg "height="/>

The inverse apostrophe can also be substituted with the "$ ()" symbol, which is equivalent

Use the result of the above command as the value of WGL

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image032 "border=" 0 "alt=" clip_ image032 "src=" http://s3.51cto.com/wyfs02/M00/75/18/wKiom1YyOIeQjpXwAABw-R_Tcd0712.jpg "height=" "/>

4.read command: This command is used for interactive method assignment and can be assigned multiple values at the same time.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image034 "border=" 0 "alt=" clip_ image034 "src=" http://s3.51cto.com/wyfs02/M01/75/18/wKiom1YyOIeTOloiAABMreq4fTo160.jpg "height=" "/>

Read-P command:-p for interactive prompt information.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image036 "border=" 0 "alt=" clip_ image036 "src=" http://s3.51cto.com/wyfs02/M02/75/18/wKiom1YyOIeT-A_JAABG1B7EnCA680.jpg "height=" "/>

4. Set the scope of the variable:

By default, a custom variable takes effect only in the shell environment of the current user and does not take effect when entering another shell environment.

For example:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image038 "border=" 0 "alt=" clip_ image038 "src=" http://s3.51cto.com/wyfs02/M02/75/16/wKioL1YyOL2T9HTnAABf33ZB7WY104.jpg "height="/>

To make a user-defined variable effective in all shells, you can use the internal command export to import the specified variable to a global variable, or you can specify multiple variable names as parameters, or you can use this command to define a new variable.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image040 "border=" 0 "alt=" clip_ image040 "src=" http://s3.51cto.com/wyfs02/M00/75/16/wKioL1YyOL2jJeExAACs9RA3l_A311.jpg "height=" 188 "/>

5. Operation of Numeric variables

The numerical operation of the Shell variable is used in the process control of the script program (such as number of cycles, usage comparison, etc.) in the Bash shell environment, only simple integer operations are performed, and fractional operations are not supported. Integer numeric operations are performed primarily through internal command expr! There must be at least one space between the operator and the variable.

+: addition operation

-: Subtraction operation

\*: Multiplication, PS: cannot be used only for "*" notation, otherwise it will be treated as a file wildcard

/: Division operation (does not calculate the number after the decimal point)

%: Modulo operations, also known as take-up operations, are used to calculate the remainder after the division is divisible (the number before the decimal point is not counted).

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image042 "border=" 0 "alt=" clip_ image042 "src=" http://s3.51cto.com/wyfs02/M01/75/16/wKioL1YyOhHzXlYEAAB-vv_mE5A896.jpg "height=" 165 "/>

6. Environment variables: Mainly used to set up the user's working environment, such as user host directory, command lookup path, user's current directory, login terminal, etc., these variables are managed by Linux system and will change with the change of user state. Use env to see the environment variables in the current working environment.

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image044 "border=" 0 "alt=" clip_ image044 "src=" http://s3.51cto.com/wyfs02/M02/75/16/wKioL1YyOhHRj7UNAAExTF9io_Q521.jpg "height=" 315 "/>

7. Positional variables: Also known as positional parameters, using "$ ... $9". For example: When executing the command line "ls-lh/boot/", where the first position variable is "-LH", denoted by "$", the second position variable is "/boot/", denoted by "$". The name of the command or script skill is denoted by "$".

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px ;p adding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image046 "border=" 0 "alt=" clip_ image046 "src=" http://s3.51cto.com/wyfs02/M00/75/16/wKioL1YyOhGg0p2YAACTmgWydXs191.jpg "height=" 194 "/>

8. Pre-defined variables: Predefined variables are pre-defined by the Bash program a class of special variables, users can only use predefined variables, cannot change, and can not be predefined variable assignment. Predefined variables are represented by the "$" symbol and another symbol combination, which is common as follows:

$#: Represents the number of positional parameters in the command line.

$*: Represents the contents of all positional parameters, that is, the name of the parameter.

$?: Represents the return status of the previous command after execution, and a return value of 0 means that execution is correct, and any non-0 value returned indicates an exception occurred.

$ A: Represents the currently executing script, process, program name.

Add: The difference between shell execution mode (source bash./)

Usage:
SOURCE file.sh or. file.sh
SOURCE command with "." Same command
What is the difference between source file.sh and SH file.sh and./file.sh execution scripts?
1. When the shell script has executable permissions, there is no difference between using sh file.sh and./file.sh to execute the script:/file.sh is because the current directory is not in path, all "." is used to represent the current directory.
2.sh file.sh re-establishes a child shell, executes the statement inside the script in the child shell, which inherits the environment variables of the parent shell, but the new, changed variables of the child shell are not brought back to the parent shell unless it is passed using export.
3.source file.sh: This command actually simply reads the statements inside the script and executes them sequentially in the current shell, without creating a new child shell. Then all the new and changed statements in the script will be saved in the current shell 650) this.width=650; "Style=" Background-image:none;border-bottom:0px;border-left : 0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" clip_image048 " Border= "0" alt= "clip_image048" src= "Http://s3.51cto.com/wyfs02/M01/75/16/wKioL1YyOhLDGXPMAAE1z9xA99A215.jpg" height= "326"/>

This article is from the "Wang Gaoli" blog, make sure to keep this source http://wanggaoli.blog.51cto.com/10422005/1707918

What is Shell,shell basics, common shell commands, usage, tricks

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.