Shell script common commands (handbook_1)

Source: Internet
Author: User
Tags bitwise

the general format of the shell and other interpreted languages?
#!/bin/bash #shell脚本的首行格式 #!/usr/bin/python #python脚本的格式 #!/usr/bin/perl #perl脚本的格式 #Description: # "#" at the beginning of a single well font is an annotation, after which content is not executed as a script command #脚本的编程内容部分, the stack of commands, a combination of single commands to complete complex tasks

How do i make a defined variable take effect?
1.~]#. /etc/profile # dot followed by definition variable file 2.~]# source/etc/profile #直接在当前shell生效
What are positional variables?
 The variable used to describe the position of the parameter. $: The name of the script itself $: the first parameter of the script, the second argument of the script ... ${10}: The 10th parameter of the script #注: From the beginning of the tenth, the parentheses $*: All parameters of the script are stored as a whole string [E Mail protected]: script all parameters, stored as a single parameter as a whole $#: Total number of all parameters in the script
How do I define a variable?
1.declare-i #定义整型-X #定义环境-R #只读变量2. Export Variable=expression #定义环境变量
How are variables referenced?
1. $variable #$ followed by variable name 2.${variable} #可加大括号
How do I invoke the execution result of a command in a script?
1.$ (command) #命令的执行结果可赋值给变量2. ' Command ' #反引号同样是命令的执行结果
How do you know which variables the system defines?
1.set #查看系统定义的所有变量2. Env/printenv #查看定义的所有环境变量
How do I cancel a defined variable?
1.unset variable# Cancel or delete a variable note: variable= #变量赋值为空不等同于变量被取消
looking at the version of Bashell?
Echo $BASH _version
Several common environment variables are:
Mail: Store mail files MailCheck: How often to patrol and send mailshlvl: How many layers are currently in Shellrandom: Software simulation to implement random number variables

1 special symbols in the shell

1.1 $ dollar sign. The value used to represent the variable. If the value of the variable name is Mike, then use $name to get the value "Mike".

1.2 # pound sign. In addition to being a prompt for Superuser, you can also make the first letter of the comment in the script, and in each line of the statement, the part that starts with the # number is not executed.

1.3 "" double quotation marks. The shell does not interpret most of the special characters in the text between pairs of double quotes, such as # is no longer the beginning of the comment, it only represents a pound sign "#". But $ still retains special meaning. (The $ plus variable name in double quotes, i.e.: $PARAM _name, is still converted to the value of the variable.) )

1.3.1 Double quotes do not work for some special symbols, for example: ", $,\," (anti-quote)

1.3.2 double quotation marks and single quotation marks cannot be nested. namely: Echo ' "" Output "", echo "" Output "

1.4 ' single quotation marks. The shell does not specifically interpret any characters between a pair of single quotes. (The $ plus variable name in double quotes, i.e.: $PARAM _name, is not converted to a variable's value.) )

1.4.1 echo "$HOME" (Result:/home/xiongguoan)

1.5 ' inverted quotation marks. Command replacement. The shell command inside the inverted quotation marks is executed first, with the result output replacing the text enclosed in quotation marks, but the special characters are interpreted by the shell.

1.5.1 Echo ' $HOME ' (Result: $HOME)

1.6 \ Slash. Used to remove the special meanings of characters in the shell interpretation. In the text, a character followed by \ is not specifically interpreted by the shell, but the rest is unaffected.

1.7 [] brackets, mostly used to test conditions, are usually placed behind an if statement. (but not quite), but the brackets themselves do not disappear after the test statement executes.

1.7.1 Echo [$HOME] (Result: Error occurred)

1.7.2 Echo [$HOME] (Result: [/home/xiongguoan]) (note: There are spaces behind HOME.) )

1.7.3 echo [$HOME –eq '/xiong '] (Result: [/home/xiongguoan–eq/xiong])

1.8 {} curly braces, mostly with the $ sign mate, are concatenated as strings to use

1.8.1 echo ${home}ismydir (Result:/home/xiongguoanismydir)

2 pre-defined variables

2.1 Special variables

The start of the shell variable name, such as $var

| pipe, transferring standard output to the standard input of the next command

$# record the number of arguments passed to the shell

# Start of comments

& Executing a process in the background

? Match one character

* Match 0 to more characters (unlike DOS, can be used in the middle of the file name, and contains.)

$-using set and the flag bits passed to the shell at execution time

$! Process number of the last child process

$? Exit status after the last command execution (return code)

$* arguments passed to the shell script

[email protected] all parameters, individually enclosed in double quotation marks.

The name of the current shell

$n (n:1-) positional parameters

$$ process identification Number (process Identifier, PID)

> Output redirection

< input redirection

>> output Redirection (Append mode)

[] lists the range of character changes, such as [A-z]

2.2-Generation value variables

* Arbitrary string

? An arbitrary character

[ABC] A, B, c one of the three

[A-n] Any character from A to n

2.3 Expression of special characters

\b Return
\c No line break when printing a line this is what we'll often use.
\f Page Change
\ r Enter
\ t watchmaking
\v Vertical Tabulation
\ \ backslash itself

2.4 Other characters

2.4.1 Semicolon

; Indicates the end of a line

2.4.2 Parentheses

() indicates that the command in parentheses is executed in the new child shell (this will not change the state of the current shell.) )

But the parentheses lose their function in single/double quotes, only as normal characters.

2.4.3 Curly Braces

Usage of the 2.4.3.1 split command

Similar to parentheses, but: 1. The commands within the curly braces are executed in the current shell; 2. Curly braces must appear as the first character of a command.

2.4.3.2 Use of reference variables

After $, indicates the start and end of the variable name

2.4.4 square brackets

Equivalent to the test command, which is used to perform testing conditions, usually after the statement that needs to be judged, for example: If,while, and so on.

3 Setting variables

3.1 Format: Varname=value (i.e. param= ' Hello ')

3.2 Note:

3.2.1 cannot have spaces before and after the equals sign

3.2.2 If the value of a variable is the result of execution of a command, add an inverse quotation mark (').

4 Reference variables

4.1 $VARNAME

4.1.1 E.I. echo $HOME (Result:/home/xiongguoan)

4.2 Variable Default value

4.2.1 can set the default value when referencing a variable. This default value is invalid if the variable is already set to a value prior to this. If the variable is not set at this time, the default value is used (but the value of this variable is not changed).

4.2.2 Echo Hello ${uname:-there} #其中there是UNAME的默认值

4.2.3 Other methods for default values and interpretation variables:

Use curly braces to denote variable substitution

Representation

Description

${variable}

Basic variable substitution. The opening and closing of the curly braces qualifying variable names

${variable:-default}

If variable does not have a value, this representation returns the value of default

${variable:=default}

If the variable has no value, this representation returns the value of default. In addition, if variable is not set, the value of default is assigned to it

${variable:+value}

If variable is set, this representation returns value; otherwise, an empty string is returned

${# VARIABLE}

This representation returns the length of the variable value, unless variable is * or @ in the special case of * or @, and returns the number of elements represented by [email protected]. Remember that $ @ Saves the list of parameters passed to the script

${variable:? MESSAGE}

If the variable does not have a value, this representation returns the value of the message. The shell also shows the name of variable, so this form is useful for catching errors

4.2.4 Note:

4.2.4.1 use ${valiable:? MESSAGE}, if the variable is found to have no value at this time, the script stops running and displays the line number and variable name. Mainly used for debugging.

4.2.4.2

5 Position variable

5.1 Using $1,$2,$3...$9,${10},${11} ... To represent the input parameters. Where $ A represents the name of the command or script being executed. $1,$2 ... Represents the input of the 1th, 2 ... A parameter

5.2 Examples:

# Cat Count.sh#!/bin/sh

A=$1 # Reads the value of a position of $, and assigns it to the variable a

B=$2 # Reads the value of the position, and assigns it to the variable B

c=$[$A + $B] # Adds the value of the variable A to B and assigns the result to C

Echo $C # shows the value of C

Results:

#./count.sh 3 6

9

#./count.sh 34 28

62

5.3 [email protected] and $* for a list of parameters, $ #代表参数的数量 (do not know the difference between [email protected] and $*)

6 Operators and Precedence

Shell operators and their order of precedence

Level

Operator

Description

13

-, +

Monocular negative, Monocular positive

12

!, ~

logical non, bitwise inverse, or complement

11

* , / , %

Multiply, divide, take modulo

10

+, -

Add, Subtract

9

<<, >>

Bitwise left SHIFT, bitwise right SHIFT

8

< =, > =, <, >

Less than or equal to, greater than or equal to, less than, greater than

7

= = , ! =

Equal to, not equal to

6

&

Bitwise-AND

5

^

Bitwise XOR OR

4

|

Bitwise OR

3

&&

Logic and

2

| |

Logical OR

1

=, + =,-=, * =,/=,% =, & =, ^ =, | =, << =, >> =

Assignment, operation, and assignment

Shell script common commands (handbook_1)

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.