Shell parentheses, square brackets, use double brackets and double square brackets scene summary

  • Foreword

    In a recent study Shell scripting, others found the program if-then block there is a conditional statement if the double parentheses (()), double brackets [[]] of use, and thus access to relevant information, but also to see to a good blog, we made a summary of the use Shell script brackets, hereby record and share

  • 1. Figures in parentheses ()

  • Brackets generally used interchangeably in the command, use the dollar sign $ cooperate, such as

    #!/bin/bash
    # 输出今年的年份
    year=$(date +%Y)
    echo "This year is $year"
    

  • 2. Square brackets []

  • Since the if-then statement can not test conditions other than the command status code, it provides Bash Shell test command for assisting if-then statement testing of other conditions such as the comparison value, compare strings, file comparison, etc. , and the test commands is shorthand square brackets [] , which must be put spaces before the first and second square brackets square brackets, otherwise it will error

  • 2.1 numerical comparison

Compare description
n1 -eq n2 Check n1 and n2 are equal
n1 -ge n2 Check if n1 n2 is greater than or equal to
n1 -gt n2 Check if n1 is greater than n2
n1 n2 -The Check n1 n2 is less than or equal to
n1 -lt n2 Check n1 is less than n2
n1 -ne n2 Check n1 n2 not equal
  • Examples are as follows:

    #!/bin/bash
    # 1. 数值比较
    n1=20
    n2=10
    if [ $n1 -ge $n2 ]; then
        echo "n1 is greater than or euqal to n2"
    else
        echo "n1 is less than n2"
    fi
    
  • Note: Bash Shell can only deal directly with integer, floating point numbers will be assigned an error

  • 2.2 String comparison

Compare description
str1 = str2 Check that same str1 and str2
str1 != str2 Check that str1 and str2 different
str1 < str2 Check if str1 str2 smaller than
str1 > str2 Check str1 is greater than str2
-n str1 Check whether the length of the non-0 str1
-with str1 Check whether the length of str1 0
  • Examples are as follows:

    # 2. 字符串比较
    user=root
    if [ $(whoami)=$user ]; then
        echo "root is online"
    else
        echo "root is offline"
    fi
    
  • File Compare 2.3

Compare description
-d file Check the file exists and is a directory
-e file Check the file if there is
-f file Check the file exists and is a file
-r file Check whether the file exists and read
-s file Check the file if there is not empty
-w file Check whether the file exists and write
-x file Check whether the file exists and execute
-O file Check the file exists and the current user belongs to all
-x file Check whether the file exists and execute
-G file Check the file exists and the current user default group same
file1 file2 -nt Check if file1 is newer than file2
file1 file2 -ot Check if file1 is older than file2
  • Examples are as follows:

    # 3. 文件比较
    fileName=test3
    if [ -e $fileName ]; then
        echo "$fileName  exists"
    else
        echo "$fileName doesn't exists"
    fi
    

  • 3. double parentheses (())

  • Dual brackets allow use in comparative statement Advanced Math

symbol description
val++ After the increase
val-- After Less
++val To increase
--val First cut
Logical negation
Bitwise negation
** Exponentiation
<< Left Shift
>> Right shift
& Boolean AND
| Boolean or
&& Logic and
|| Logical or
  • Examples are as follows:
    #!/bin/bash
    # 双括号使用练习
    var1=10
    if (($var1 >= 10)); then
        for ((i = 0; i < 3; i++)); do
            echo $i
        done
    fi
    

  • 4. double square brackets [[]]

  • The two sides brace provides advanced features for string comparisons, mathematical symbols can be used to compare strings, and implements pattern matching

    #!/bin/bash
    # 双方括号使用练习
    fileName=test5
    if [[ $fileName==test* ]]; then
        echo "This is a test file!"
        if [[ $fileName==test5 ]]; then
            echo "This file is test5!"
        fi
    fi
    
  • Note: Not all Shell supports both the brackets


  • 5. References

"Linux command-line shell and scripting Encyclopedia (third edition)"

Released seven original articles · won praise 4 · views 10000 +

Guess you like

Origin blog.csdn.net/TomAndersen/article/details/104214632