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 test command in Shell

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to use the test command in Shell. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

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

Example demonstration:

Num1=100num2=100if test $[num1]-eq $[num2] then echo 'two equal numbers!' Else echo 'two numbers are not equal!' Fi

Output result:

The two numbers are equal!

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

#! / bin/basha=5b=6result=$ [aqb] # Note that there can be no spaces on both sides of the equal sign echo "result is: $result"

The result is:

Result is: 11

String test

If the parameter description = equals, it is true! = unequal, it is true-if the length of the z string string is zero, it is true-n string string length is not zero, it is true

Example demonstration:

Num1= "ru1noob" num2= "runoob" if test $num1= $num2then echo 'two strings equal!' else echo 'two strings are not equal!' fi

Output result:

Two strings are not 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 is true-d filename if True-f file name if the file exists and is a directory, 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

Example demonstration:

Cd / binif test-e. / bashthen echo 'file already exists!' else echo 'file does not exist!'

Fi

Output result:

The file already exists!

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

Cd / binif test-e. / notFile-o-e. / bashthen echo'at least one file exists! 'else echo' neither file exists'fi'

Output result:

At least one file exists! This is the end of this article on "how to use test commands in Shell". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Servers

Wechat

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

12
Report