5, shell- operator

1. The basic syntax

( 1 ) "$ (( expression ))" or "$ [ expression ]"

( 2 ) expr +, -, \ *, /,%     add , subtract , multiply, divide, modulo

Note : expr between operators have spaces

2. Case practical operation:

(1) calculate 3 2 + value

[atguigu@hadoop101 datas]$ expr 2 + 3

5

(2) calculation of 3-2 values

[atguigu@hadoop101 datas]$ expr 3 - 2

1

(3) calculating ( 2 + 3 ) X4 value

( A ) expr one step calculation

[Atguigu @ hadoop101 datas] `$ expr expr 2 + 3` \ * 4

20

( B ) using the $ [ expression ] mode

[atguigu@hadoop101 datas]# S=$[(2+3)*4]

[atguigu@hadoop101 datas]# echo $S

The first 6 chapters conditional

1. The basic syntax

[Condition] ( Note that condition before and have spaces )

Note : is the non-null condition to true , [atguigu] Returns to true , [] returns to false .

2. Common determination condition

( 1 ) Comparison between two integers

= String comparison

-lt  less than (less than) -le less ( less equal)

-eq equal (equal) -gt greater than (greater than)

-ge greater than or equal ( Greater equal) -ne not equal (Not equal)

( 2 ) is judged according to file permissions

-r have read permissions ( the Read ) -w have the authority to write ( the Write )

-x have execute permissions (execute)

( 3 ) the determination by file type

-f file exists and is a regular file ( File )

-e file exists (existence) -d file exists and is a directory (directory)

3. Case practical operation

( 1 ) 23 is greater than equal to 22

[atguigu@hadoop101 datas]$ [ 23 -ge 22 ]

[atguigu@hadoop101 datas]$ echo $?

0

( 2 ) helloworld.sh whether the write permission

[atguigu@hadoop101 datas]$ [ -w helloworld.sh ]

[atguigu@hadoop101 datas]$ echo $?

0

( 3 ) /home/atguigu/cls.txt directory file exists

[@ Atguigu hadoop101 dates] $ [-e /home/atguigu/cls.txt]

[atguigu@hadoop101 datas]$ echo $?

1

(4) Multi- condition determination (&& indicates a command before a command is successful, before the implementation, || represents the one command fails, the next command only )

[atguigu@hadoop101 ~]$ [ condition ] && echo OK || echo notok

OK

[Atguigu @ hadoop101 datas] $ [condition] && [] || echo otok

full-blooded

Guess you like

Origin www.cnblogs.com/liuzhonghui/p/12001325.html