Cigarette Smoker---------Linux Bash Basic feature conditions Test && custom Exit status code (6)

Source: Internet
Author: User

Condition test

To determine whether a demand is satisfied, it needs to be implemented by a testing mechanism.

different test conditions are expressed according to the execution status result of the command


1, according to the execution status of the ID command results, determine whether the user exists

[[email protected] ~]# ID rootuid=0 (root) gid=0 (root) groups=0 (root) [[email protected] ~]# echo $?0[[email protected] ~]# ID helpid:help:no such user[[email protected] ~]# echo $?1


2, according to find in the/etc/passwd file root of the word execution status results, determine whether the user exists

[[email protected] ~]# grep-q ' ^root\b '/etc/passwd[[email protected] ~]# echo $?0[[email protected] ~]# grep-q ' ^help\b '/etc/passwd[[email protected] ~]# echo $?1


Use a test to use a special test character when testing

1) test-tested expression

[[email protected] ~]# Test 1 > 3[[email protected] ~]# echo $?0[[email protected] ~]# Test 1-GT 3 # in the numerical test, use the-GT expression ; symbols [[email protected] ~]# echo $?1

2) [Test expression]

[[Email protected] ~]# [1-LT 3] # test expression and brackets must have a space, otherwise you will get an error. -bash: [1:command not Found[[email protected] ~]# [1-LT 3] # in the numeric test, use the-LT notation < symbol [[email protected] ~]# echo $?0

3) [[Test expression]]

[[Email protected] ~]# [[1-lt 3]]-bash: [[1:command not Found[[email protected] ~]# [[1-eq 3]] # in the case of a numerical test, the = symbol is denoted by the-eq [Email protected] ~]# echo $?1


Test character classification

Numerical test

-eq left is equal to right

-ne is not equal to

-GT is greater than

-ge is greater than or equal to

-lt is less than

-le is less than or equal to

1) A-eq B, test whether the value represented by a is equal to the value represented by B. is equal to True

# #测试方法一 # a=1# b=10# Test $B-eq $A # test variable B stored data in memory space and variable A the data stored in the memory space is equal # echo $?1# #测试方法二 # [$B-eq $A]# echo $?1# #测试方法三 # [[$B-eq $A]]# Echo $?1

2) A-ne B, test whether the value represented by a is not equal to the value represented by B. Not equal to True

# #测试方法一 # test $B-ne $A # The data stored in variable B memory space and the data stored in variable A memory space are not equal # echo $?0# #测试方法二 # [$B-ne $A]# Echo $?0# #测试方法三 # [[$B-ne $A]]# Echo $?0

3) A-gt B, test whether the value represented by a is greater than the value represented by B. is greater than true

# #测试方法一 # Test 10-GT 3 # Testing 10 is greater than 3# echo $?0# #测试方法二 # [10-GT 3]# Echo $?0# #测试方法三 # [[10-GT 3]]# Echo $?0

3) A-ge B, test whether the value represented by a is greater than or equal to the value represented by B. Greater than or equal to true

# #测试方法一 # Test 10-ge 3 # Testing 10 is greater than or equal to 3# echo $?0# #测试方法二 # [10-ge 3]# Echo $?0# #测试方法三 # [[10-ge 3]]# Echo $?0

4) A-lt B, test whether the value represented by a is less than the value represented by B. is less than true

# #测试方法一 # Let 1-LT 3 # test 1 is less than 3# echo $?0# #测试方法二 # [1-LT 3]# Echo $?0# #测试方法三 # [[1-lt 3]]# Echo $?0

5) A-le B, test whether the value represented by a is less than or equal to the value represented by B. is less than or equal to true

# #测试方法一 # Let 1-le 3 # test 1 is less than 3# echo $?0# #测试方法二 # [1-le 3]# Echo $?0# #测试方法三 # [[1-le 3]]# Echo $?0

Character Test ( all operands should use references )

= = equivalent Test

> is greater than, ASCII code comparison

< is less than

! = is not equal to

A =~ whether the left string of the pattern can be matched to the right (regular expression)pattern . typically used in [[]], pattern cannot be quoted

-Z "string" test string is empty, empty is true

-N "STRING" is not empty

1) A = = B, the characters represented by a and B are equal

# #测试方法一 # test "Obama" = "Obama" # test the left string equal to the right string # echo $?0# Test "Obama" = = "Obama" # visible string, must complete equal # echo $?1# a=ob  ama# b=jerry# Test "$A" = = "$B" # Tests if the data stored in a variable memory space (character or numeric) is equal to the data stored in the B variable # echo $?1# #测试方法二 # ["Obama" = "Obama"]# Echo $?0# a=obama# b=jerry# ["$A" = "$B"]# Echo $?1# #测试方法三 # [["Obama" = = "Obama"]]# echo $?0# [["$A" = = "$B"]]# echo $? 1

2) a > B, whether the character represented by a is greater than the character represented by B

3) A < B, whether the character represented by a is less than the character represented by B

4) A! = B, the character represented by a and whether the character represented by B is unequal

# #测试方法一 # test "Obama"! = "Obama" # test the left string, whether it is not equal to the right string # echo $?1# Test "$B"! = "$A" # echo $?0# #测试方法二 # ["Obama"! = "Obam  A "] # echo $?1# [" $B "! =" $A "] # echo $?0# #测试方法三 # [[" Obama "! =" Obama "]]# echo $?1# a=obama# b=jerry# [[" $A "! =" $B " ]]# Echo $?0

5) A =~ B, the character represented by a can be matched by the (regular expression) pattern represented by B .

# test "Obama" =~ "^o.*"-bash:test: =~: binary operator expected# test "Obama" =~ ^o.*-bash:test: =~: binary operator ex pected# ["Obama" =~ "^o.*"]-bash: [: =~: binary operator expected# ["Obama" =~ ^o.*]-bash: [: =~: binary operator Expe cted# [["Obama" =~ "^o.*"]]# echo $?1# [["Obama" =~ ^o.*]] #模式不能加引号 # echo $?0

6)-Z "B", test whether the string represented by B is empty, NULL is True

# #测试方法一 # test-z "Obama" # echo $?1a=# test-z "$A" # A variable is empty, test its result # echo $?0a=jerry# test-z "$A" # A variable is not empty, test its result # echo $? # #测试方法二 # [-Z ' How is You? '] # echo $?1a=# [-z] $A "]# Echo $?0# #测试方法三 # [[-Z ' How is it?] # test string is empty # echo $?1a=# [[-Z ' $A]] # Test the string stored in the variable, whether is empty # echo $?0

7)-n "b", test B to indicate whether the string is not empty, not empty is true

# #测试方法一 # Test-n "Obama" # echo $?0a=# test-n "$A" # A variable is empty, test its result # echo $?1a=jerry# test-n "$A" # A variable is not empty, test its result # echo $? 0# #测试方法二 # [-N ' How is You? '] # echo $?0a=# [-n ' $A ']# echo $?1# #测试方法三 # [[-N ' How is it? '] # test string is empty # echo $?1a=# [[-N ' $A]] # Test the string stored in the variable, whether is empty # echo $?1

File test



2. Custom exit status code in bash

Exit [#] in the bash script, once the exit command terminates the script, the exit status code is the value after exit.

1. When exit is present

# nano helo.sh#!/bin/bashprintf "Hello everyone\n" Exit 1## determine the status result of script execution # bash helo.sh Hello everyone# echo $?1

2. Exit does not exist

# nano helo.sh#!/bin/bashprintf "Hello everyone\n" # # to determine the status result of script execution # bash helo.sh#!/bin/bashls/varrprintf "Hello everyone\   N "# echo $? # #返回值为最后一个命令的执行状态结果0


This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1953085

Cigarette Smoker---------Linux Bash Basic feature conditions Test && custom Exit status code (6)

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.