Variable test of Shell entry

Source: Internet
Author: User
Tags shebang

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

Related Article

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.