很多时候在Shell里面用`if`的时候后面需要写各种神奇的方括号,这些方括号里面还会有各种参数来控制具体要判断的是什么。 如果不查手册,有时候写出来的东西就容易出问题。比如经常就会搞混单方括号和双方括号的对于数值相等判断的表达式写法。还有各种傻傻分不清的判空、判断文件、判断目录的参数。 所以为何不直接把这些乱七八糟的都丢了,直接换成更接近自然语言的写法呢? ```bash # Numbers and expressions if that expression 1 = 1; then echo EQUAL; fi # true if that exp 1 = 1; then echo EQUAL; fi # true if that 1 = 1; then echo EQUAL; fi # true if that expression 1 + 1 \> 0 is true; then echo TRUE; fi # true if that exp 1 + 1 \> 0 is true; then echo TRUE; fi # true if that 1 + 1 \> 0 is true; then echo TRUE; fi # true if that expression 1 + 1 is greater than 0; then echo TRUE; fi # true if that exp 1 + 1 is greater than 0; then echo TRUE; fi # true if that 1 + 1 is greater than 0; then echo TRUE; fi # true # Strings if that string "STRING" is same as "STRING"; then echo SAME; fi # true if that str "STRING" = "STRING"; then echo SAME; fi # true if that empty ""; then echo EMPTY; fi # true # File and directory checks if that file "./file.txt" exist; then echo EXIST; fi # true if that file "./directory/" exist; then echo EXIST; fi # true if that directory "./file.txt" exist; then echo DIR; fi # false if that dir "./directory/" exist; then echo DIR; fi # true # File and directory type checks. # Use `is a file` is same as `is file` if that file "./file.txt" is a file; then echo FILE; fi # true if that file "./file.txt" is file; then echo FILE; fi # true # Asking for a file that not existed for type checking results in ERROR. # Use conditional chaining to check file exist first. if that file "./nonexistent" is a file; then echo FILE; fi # ERROR # Directory is a type of file, so leading command `file` wont affect the result. # You can use `dir` for a short term if that file "./directory/" is a file; then echo FILE; fi # false if that file "./directory/" is a directory; then echo DIR; fi # true if that file "./directory/" is a dir; then echo DIR; fi # true # The leading command "directory" will change the behaviour on `file` check. # When you ask for something that should be a directory is a file, it will always return a error. if that directory "./file.txt" is a file; then echo FILE; fi # ERROR if that directory "./directory/" is a directory; then echo DIR; fi # true # the `directory` in both command and check can be shorten to `dir`. if that dir "./directory/" is a dir; then echo DIR; fi # true # Chaining multiple conditions # By default conditional logical operations only work on conditions, not expression. # You can have optional comma between conditions. if that 1 = 1 and 2 = 2; then echo ALL_EQUAL; fi # true if that 2 is greater than 3, or 3 is greater than 2; then echo OR; fi # true if that file "./file.txt" exist, and is file; then echo FILE; fi # true # Symlinks if that file "./filesymlink" is a symlink; then echo SYMLINK; fi # true if that file "./dirsymlink" is a symlink; then echo SYMLINK; fi # true if that directory "./filesymlink" is a symlink; then echo SYMLINK; fi # ERROR if that directory "./dirsymlink" is a symlink; then echo SYMLINK; fi # true if that directory "./dirsymlink" is a directory; then echo DIR; fi # true if that directory "./dirsymlink" is a file; then echo DIR; fi # false if that file "./dirsymlink" is a directory; then echo DIR; fi # true if that file "./filesymlink" is a directory; then echo DIR; fi # false if that file "./filesymlink" is a file; then echo FILE; fi # true ```