Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Shell script test command

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report