Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Shell script test command

Shulou Source: shulou.com Published: 2022-06-03 08:22:12 11月14日 Update

This article mainly explains "how to use Shell script test command", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "how to use Shell script test command" bar!

The test command in Shell is used to check whether a condition is true, and it can be tested in terms of values, characters, and files.

Numerical test

The parameter states that-eq equals to true-ne is not equal to true-gt greater than or equal to true-ge greater than or equal to true-lt less than or equal to true-le less than or equal to true

For example:

The code is as follows:

Num1=100

Num2=100

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

Then

Echo 'The two numbers are equalizer'

Else

Echo 'The two numbers are not equalizer'

Fi

Output:

The two numbers are equal!

String test

Parameter description = equal to true! = unequal is true-z string length false is true-n string length is true

For example:

The code is as follows:

Num1=100

Num2=100

If test num1=num2

Then

Echo 'The two strings are equalizer'

Else

Echo 'The two strings are not equalizer'

Fi

Output:

The two strings are equal!

File testing

Parameter description-e filename if the file exists, true-r filename if the file exists and readable, true-w filename if the file exists and writable, true-x filename if the file exists and executable, true-s filename if the file exists and at least one character, true-d filename if the file exists and is a directory, true-f filename if the file is saved True-c file name if the file exists and is a character-type special file, true-b file name if the file exists and is a block special file

For example:

The code is as follows:

Cd / bin

If test-e. / bash

Then

Echo 'The file already slots'

Else

Echo 'The file does not slots'

Fi

Output:

The file already exists!

In addition, Shell provides three logical operators with (!), or (- o), and non (- a) to connect test conditions, with a priority of: "!" The highest, the second is "- a" and the lowest is "- o". For example:

The code is as follows:

Cd / bin

If test-e. / notFile-o. / bash

Then

Echo 'One file exists at leastmasters'

Else

Echo 'Both dose not slots'

Fi

Output:

One file exists at least!

Thank you for your reading, the above is the content of "how to use Shell script test command", after the study of this article, I believe you have a deeper understanding of how to use Shell script test command, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: File file name character command string test script code output parameter learning special three content numeric value condition length character type normal minimum Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Microsoft Shulou Tech Info OPPO Reno Apple