Linux Basic Series-day9

Source: Internet
Author: User
Tags arithmetic operators logical operators

Arithmetic operators

Set Variable a=10,b=4

+ Addition

[[Email protected] ~] # echo $[$a + $b]14

-Subtraction

[[Email protected] ~] # echo $[$a-$b]6

* Multiplication

[[Email protected] ~] # echo $[$a * $b]40

/division, because is divisible, so only equals 2

[[Email protected] ~] # echo $[$a/$b]2

% take-up

[[Email protected] ~] # echo $[$a% $b]2

In scripts, you can use expr to call

[[Email protected] ~] # echo ' expr $a + $b '~]#  echo ' expr $a-$b '6~]#  echo ' expr $a \* $b '+~]#  echo ' expr $a/$b '2~]#  echo ' expr $a% $b ' /c15>2


Assignment operators

= Assignment, used to assign values to variables

[[Email protected] ~] # a=5 [[Email protected] ~] # b= $a [[Email protected] ~] # Echo $b5

+ = variable is assigned after addition operation

[[Email protected] ~] # x=0 [[Email protected] ~] # ((x+=5)) [[Email protected] ~] # Echo $x5

*= variable is assigned after multiplication method

[[Email protected] ~] # x=5 [[Email protected] ~] # ((x*=5)) [[Email protected] ~] # Echo $x25

/= variables are assigned after the division method is evaluated

[[Email protected] ~] # x=5 [[Email protected] ~] # ((x/=2)) [[Email protected] ~] # Echo $x2

%= variables are assigned after the remainder operation

[[Email protected] ~] # x=5 [[Email protected] ~] # ((x%=2)) [[Email protected] ~] # echo $x1

String relational operators

= = Compares two strings for equality, equals true

[[Email protected] ~] # A=ABC [[Email protected] ~] # B=ABC [[Email protected] ~] # [[$a = = $b]] [[Email protected] ~] #      echo $? #  $? Returns the execution status of the previous command, 0 is true  0

! = Compare two strings do not want to wait, do not want to wait for the true

[[Email protected] ~] # A=ABC [[Email protected] ~] # B=abb [[Email protected] ~] # [[$a = = $b]] [[Email protected] ~] # echo $? 1~]#  [[$a! = $b]][[email protected] ~]#  echo $? 0

Integer relational operators

Format: [integer operator integer]

-eq equals

[[Email protected] ~] # [5-eq 5] [[Email protected] ~] # echo $? 0

-GT Greater than

[[Email protected] ~] # [8-GT 5] [[Email protected] ~] # echo $? 0

-lt less than

[[Email protected] ~] # [3-lt 5] [[Email protected] ~] # echo $? 0

-ge greater than or equal to

[[Email protected] ~] # [5-ge 5] [[Email protected] ~] # echo $?  ~]#  [5-ge 3][[email protected] ~]#  echo $? 0

-le less than or equal to

[[Email protected] ~] # [3-le 3] [[Email protected] ~] # echo $?  ~]#  [3-le 5][[email protected] ~]#  echo $? 0

-ne Not equal to

[[Email protected] ~] # [3-ne 5] [[Email protected] ~] # echo $? 0

Comparison operations for files

-nt (newer than) to determine whether File1 is newer than file2

[Email protected] ~]# [abc.txt-nt aaa.txt][[email protected] ~]# echo $?1

-ot (older than) determine if file is older than file2

[Email protected] ~]# [Abc.txt-ot aaa.txt][[email protected] ~]# echo $?0

-ef judge whether File1 and File2 are the same file, can be used in judging hard link

[Email protected] ~]# [Abc.txt-ef aaa.txt][[email protected] ~]# echo $?1

logical operators

-A or &&: logical AND, "and" means, before and after two expressions are all established, the entire test result is true, otherwise false

[[Email protected] ~]#a=3[[Email protected] ~]#b=6[[Email protected] ~]#[$a-ne 6] && [$b-gt 9][[Email protected] ~]#echo $? #因为a不等于6, B is not greater than 9, so the left is set up, the right is not set up, so the overall is not established 1[[Email protected]~]#[$a-ne 6] && [$b-gt 4][[Email protected] ~]#echo $? #因为a不等于6, B is more than 4, around are set up, so the overall establishment 0
[[Email protected] ~]# [$a-ne 6-a $b-gt 4]
[[Email protected] ~]# echo $?
0

-O or | | : Logical OR, "or" means that at least one of the two sides of the operator is true, the result is true, and no is false

[[Email protected] ~]#a=3[[Email protected] ~]#b=6[[Email protected] ~]#[$a-ne 6] | | [$b-GT 9][[Email protected] ~]#echo $? #因为左边a不等于6成立, so the whole establishment 0[[email protected]~]#[$a-ne 3] | | [$b-GT 4][[Email protected] ~]#echo $? #因为右边b大于4成立, so the whole establishment 0[[email protected]~]#[$a-ne 3] | | [$b-GT 9][[Email protected] ~]#echo $? #因为左右两边都不成立, so the whole is not established 1

! : Logical No, when the conditions are not set, the return result is true

[[Email protected] ~] # [! $a-ne 3] | | [$b-GT 9] [[Email protected] ~] #    echo $? #因为左边a等于3, after-ne judgment for false, and after! The negation of the false, so for the true establishment  0

Test action

Test can be replaced with [], such as Test-e Abc.txt can be written as [-e Abc.txt]

File type judgment

-e Whether the file name exists

-f The filename exists and is file

-D whether the file name exists and is a directory

-B Whether the file name exists and is a block

-C whether the file name exists and is a character device

-S The file name exists and is a socket file

-P The file name exists and is a FIFO (pipe) file

-l The file name exists and is a connection file

Example:

[[Email protected] ~] # Touch Abc.txt [[Email protected] ~] #  [[email protected] ~]#  echo $?  ~]#[[email protected] ~]#  echo $? 1~]#[[email protected] ~]#  echo $? 0

File Permission Detection

-R detects if the file name exists and has "readable" permissions

-W detects if the file name exists and has "writable" permissions

-X detects if the file name exists and has "executable" permission

-U detects if the file name exists and has "SUID" permission

-G detects if the file name exists and has "SGID" permission

-K detects if the file name exists and has the "Sticky bit" permission

-S detects if the file name exists and is "not a blank file"

Example:

[[Email protected] ~] #  [[email protected] ~]#  echo $?  ~]#[[email protected] ~]#  echo $? 1

String Data judgment

-Z to determine if the string is 0, or null to return True (0)

[[Email protected] ~] # test-z "ABC" [[Email protected] ~] # echo $? 1

-N to determine if the string is 0, or null to return False (0)

[[Email protected] ~] # test-n "ABC" [[Email protected] ~] # echo $? 0

Process Control

If single branch: performs the appropriate action when the condition is set.

if condition test operation then command sequence fi

Example: Output True when 100 is greater than 80 o'clock

if [100-gt ];then     " True " fi

If double branch: performs different actions when the condition is set and the condition is not valid.

if condition Test Command then    command sequence 1else     command sequence 2fi

Example: Output true when checking to presence abc.txt, otherwise output false

# !/bin/bash if [-e abc.txt];then     " True " Else  "False" fi     

If multi-branch: Equivalent If statement nesting, performing different operations on multiple conditions.

if condition Test command 1; then    command sequence 1elif  Condition test command 2;then    command sequence 2elif  ...  Else     command sequence NFI

For loop: Performs a set of command operations repeatedly based on the different values of the scalar.

 for inch Value list do    command sequence done

Example 1: Printing numbers 1 through 5 sequentially

# !/bin/bash  for inch [1..5]do    

While condition loop: repeats the condition of the test instruction, and executes the corresponding command operation repeatedly, as long as the condition is true, until the condition is false. If used true as a loop condition can produce an infinite loop.

 while command-expression do    command list done

Example: adding 20 system accounts in bulk user name is USER1~20

# !/bin/bashi=1 while [$i-le ]do    useradd user$1    "123456 " | passwd--stdin user$i &>/dev/null    i= ' expr $i + 1' done

Linux Basic Series-day9

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.