Format: Test condition
String test: Note space: Test STR1 = = STR2 Tests if string is equal test str1! = str2 test string is not equal test STR1 tests if string is not empty test-n str1 test string is not empty test-z str1 Test whether the string is an empty integer testInt1-eqInt2Tests whether integers are equal testInt1-geInt2TestInt1Whether >=Int2TestInt1-gtInt2TestInt1Whether >Int2TestInt1-leInt2TestInt1Whether <=Int2TestInt1-ltInt2TestInt1Whether <Int2TestInt1-neInt2Test if two numbers are not equal file test test-dfileSpecifies whether the file is a directory test-ffileSpecifies whether the file is a regular file test-xfileSpecifies whether the file can be executed Test-rfileSpecifies whether the file is readable test-wfileSpecifies whether the file can write test-a fileSpecifies whether the file exists test-sfileSpecifies whether the file size is non-0
Test statements are generally not used alone, generally as test conditions for an if statement, such as:
if"hello""hello" ;thencommands....fi上面语句也可简化为(注意[]与"之间的空格)if [ "hello" == "hello" ];then....
Look at the code:
#!/bin/bashifTest"Hello"=="Hello"; ThenEcho "equals"ElseEcho "Not equals"fiifTest-z""; ThenEcho "STR is null"fiifTest-n""; ThenEcho "STR is not NULL"fiifTest"9"; ThenEcho "NOT NULL"ElseEcho "NULL"fi#easyif["Hello"=="Hello"]; ThenEcho "equals"ElseEcho "Not equals"fiif[- F/ROOT/TEST/TEST1]; ThenEcho "Test1 is a file"elif[- D/ROOT/TEST/TEST1]; ThenEcho "Test1 is a dir"ElseEcho "I don ' t know the result"fi
Execution effect:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Variable test of Shell entry