Shell Test Command

Source: Internet
Author: User
Tags logical operators

The test command in the Shell is used to check if a condition is true, and it can be tested in three aspects of numeric, character, and file.

Numerical test

Parameter description

-eq equals True

-ne is not equal to true

-GT is greater than true

-ge is true if it is greater than or equal

-lt is less than true

-le is less than or equal to true

Example Demo:

num1=100

num2=100

if test $[num1]-eq $[num2]

Then

Echo ' Two numbers equal! ‘

Else

Echo ' Two numbers not equal '

Fi

Output Result:

Two numbers equal!

The [] in the code performs basic arithmetic operations such as:

#!/bin/bash

A=5

B=6

RESULT=$[A+B] # Note that there must be no spaces on either side

echo "Result: $result"

The result is:

Result is: 11

String test

Parameter description

= Equals is True

! = True if not equal

-Z String string is true if the length is zero

-N String string is true if the length is nonzero

Example Demo:

num1= "Rulnoob"

Num2= "Runoob"

if test $num 1 = $num 2

Then

Echo ' Two strings equal! '

Else

Echo ' two strings are not equal! '

Fi

Output Result:

Two strings are not equal!

File test

Parameter description

-e filename stating that the file exists is true

-r file name True if the file exists and is readable

-W file name True if the file exists and is writable

-X file name if file exists and executable is true

-d file name if the file exists and is true for the directory

-f filename If the file exists and is a character special file is True

-B file name True if file exists and is a block special file

Example Demo:

Cd/bin

If Test-e./bash

Then

Echo ' file already exists! '

Else

Echo ' file does not exist! '

Fi

Output Result:

File already exists!

In addition the Shell is provided with (-a) or (-O) non (! Three logical operators are used to link test conditions, with the priority being:! Highest-a second-o lowest

Shell Test Command

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.