(E) shell variable declaration and use

10: Shell operator

Method (1) numerical operations

What if I need a numerical calculation, you can use any one of three ways: using declare declare a variable type, since all variables default type is string, so long as we declare a variable as integer arithmetic can not Yet? Use the command to declare the variable type declaration can be achieved. Command is as follows:

[Root @ localhost ~] # declare [+/-] [options]

Variable name options:

-: type attribute set to a variable

+: Type attribute variable canceled

-a: variable declared as an array type

-i: variable declared as an integer type (integer)

-r: speak variable declared as read-only variables. Note that, once the read-only variables, we can not modify the value of a variable, the variable can not be deleted, even not canceled by + r read-only attribute.

-x: Declaring a variable as environment variable

-p: Specifies the type of display variable is declared

 

Example 1: declare variable type computes

[root@localhost ~]# aa=11

[root@localhost ~]# bb=22

# Assigned to the variable aa and bb

[root@localhost ~]# declare -i cc=$aa+$bb

Type cc # declare variables of type integer, and its value is aa and bb

[root@localhost ~]# echo $cc

33

# This time finally summed up

 

Example 2: array variable type

Note that the array index starts from 0, and when calling array values, required {$ array [index]} way to read.

[root@localhost ~]# name[0]="li "

 [root@localhost ~]# name[1]="ming"

 [root@localhost ~]# name[2]=" ang"

 [root@localhost ~]# echo ${name}

at the

# Output array of content, if just write the name of the array, then only the output of the first index variable

[root@localhost ~]# echo ${name[*]}

li ming ang

 

Example 3: Environment Variables

In fact, we can also use the command to declare the variable is declared as an environment variable, and the role of export orders is the same:

[root@localhost ~]# declare -x test=123

# Declare the variable test environment variable

 

Example 4: read-only attribute

Note that once the read-only attribute is set to a variable, then this variable can neither change the value of the variable, nor can you delete variables can not even use the "+ r" option to cancel the read-only attribute. Command is as follows:

[root@localhost ~]# declare -r test

# Gives read-only attribute to test

[root@localhost ~]# test=456

-bash: test: readonly variable

#Test variable value can not be changed

[root@localhost ~]# declare +r test

-bash: declare: test: readonly variable

# You can not cancel the read-only attribute

[root@localhost ~]# unset test

-bash: unset: test: cannot unset: readonly variable

# Can not delete variables.

 

 

Example 5: Query variable attributes and variable attributes canceled

Query uses a variable property "-p" option, eliminating the use of variable attributes "+" option. Command is as follows:

[root@localhost ~]# declare -p cc

declare -i cc="33"

#cc variable of type int

[root@localhost ~]# declare -p name

declare -a name='([0]=" li " [1]="li " [2]="ming")'

#name variable is an array type

[root@localhost ~]# declare -p test

declare -rx test="123"

#test environment variables and variables are read-only variables

[root@localhost ~]# declare +x test

# Cancel test variable environment variable properties

[root@localhost ~]# declare -p test

declare -r test="123"

# Note that the read-only attribute variables can not be canceled.

 

Examples 6: shell operation

Shell arithmetic calculation performed:

(1) $ (($ num1 + $ num2))

(2) declare -i is shaping declare variables

(3)dd=$(expr $aa + $bb)

Value #dd is aa and bb and. Note that "+" about the number must be a space on both sides

(4) let ee = $ aa + $ bb 

(5)gg=$[ .

+ $ B]

 

Shell operator:

13-- + unary minus, n monocular

12!, ~ Non-logical, or bitwise complement

11 *, /,%, multiply, divide, modulo

10 + - Add, Subtract

9 << >> Bitwise left, Bitwise Right Shift

8 <=,> =, <,> less than or equal to, greater than, or equal to, less than, greater than

7 ==,! = Equal, not equal

/ * & Bitwise AND /

5 ^ bitwise XOR

4 | Bitwise or

3 && Logical AND

2 || logical OR

1 =, + =, - =, * =, / =,% =, & =, ^ =, | =, << =, >> = assignment, calculation and Fu

Substantially: Commonly used four operations, &&, ||,

XOR operation: a binary operation, & 0 is 0, | 1 of 1

 

Testing and contents of the variable displacement:

Test whether a variable has a value:

Happening:

(1) no variable declarations, echo output is empty. set -u echo $ name is not being given, recognition programs need to use the shell's own test

(2) variable declaration name = "" is empty

x = $ {y- new value new value} x = x x = $ y is empty

If you do not declare y x = new

If y = "" x = Empty

If y = 123 x = 123

Guess you like

Origin www.cnblogs.com/love-life-insist/p/11668769.html
Recommended