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

Shell Learning [test Command]

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Shell test command

The test command is mainly used to check whether the condition is established, and can be used for numeric judgment, character judgment and file judgment; you can also use your own commonly used check commands.

Test Digital Demo #! / bin/bashecho "Hello World!" num1=10num2=10# routinely judges if [${num1}-eq ${num2}] then echo 'two numbers are equal!' Else echo 'two numbers are not equal!' Fi#test routinely judges that if test $[num1]-eq $[num2] then echo 'two numbers are equal!' Else echo 'two numbers are not equal!' Fi

The numeric judge parameter states that-eq equals to true (abbreviation of equal)-ne is not equal to true (abbreviation of not equal)-gt is greater than or equal to true (abbreviation of greater than)-ge is greater than or equal to true (abbreviation of greater&equal)-lt is less than or equal to true (abbreviation of lower than)-le less than or equal to true (abbreviation of lower&equal)

Test character demonstration #! / bin/bashnum1= "aaaa" num2= "bbbb" # routine judgment if [${num1} = ${num2}] then echo 'character equality!' Else echo 'characters are not equal!' Fi#test judges if test $num1 = $num2then echo 'character equality!' Else echo 'characters are not equal!' Fi

The character judge parameter description = equal is true! if = is not equal, it is true-z string length is zero, then true-n string string length is not zero, it is true

The test file demonstrates whether the #! / bin/bash# regular file exists if [!-d $BAK_DIR]; then mkdir-p $BAK_DIRfi#test determines that the file if test-e ${BAK_DIR} then echo 'file exists!' Else echo 'file does not exist!' Fi# and (- a), or (- o), and non (!) logical operators are used to if test-e ${BAK_DIR}-o-e ${BAK_DIR1} then echo'at least one file!' Else echo'no file exists!' Fi

The file judge [test] parameter states that the file name is true if the file exists-r file name if the file exists and is readable, true-w file name if the file exists and writable, true-x file name if the file exists and executable, true-s file name if the file exists and has at least one character, true-d file name if the file exists and is a directory, true-f The file name is true if the file exists and is a normal file

Note: three logical operators, namely (- a), or (- o) and non (!), are used to connect test conditions with a priority of: "!" The highest, the second is "- a" and the lowest is "- o".

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