Linux-square brackets

Linux square brackers: “[]”
square brackets in bash-part 1
square brackets in bash-part 2

1. 通配符

  • “*“: 匹配多个任意字符
  • “\?”: 匹配单个字符
  • “[]“: 匹配范围
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #1.创建100个文件
    $ touch file0{0..9}{0..9}

    $ ls file00*
    file000 file001 file002 file003 file004 file005 file006 file007 file008 file009

    $ ls file05?
    file050 file051 file052 file053 file054 file055 file056 file057 file058 file059


    $ ls file0[123]0
    file010 file020 file030

    $ ls file0[1-3]0
    file010 file020 file030

2. Logical operators

shell基本运算

1
2
3
4
5
6
7
8
9
10
11
12
13
[ str1 = str2 ] : if the strings are equal
[ str1 != str2 ] : if the strings are not equal
[ m -eq n ] : if Integer m is equal to Integer n
[ m -ge n ] : m >= n
[ m -le n ] : m <= n
[ m -lt n ] : m < n
[ m -nt n ] : m != n

[ -e file ] : 文件是否存在
[ -f file ] : tests whether file is a file(exists) or not a file
[ -d file ] : tests whether file is a directory
[ -h file ] : tests whether file is a symbolic link
[ -G file ] : tests whether file belongs to a certain group

运算示例:

1
2
3
4
5
6
7
8
9
$ if [ -f file001 ] ; then echo "Good"; else echo "Bad";fi
Good

$ if [ ! -f file001 ]; then echo "Good";else echo "Bad";fi
Bad

$ mkdir hell
$ if [ -d hell ] ; then echo "Directory";else echo "Not Directory";fi
Directory