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

Conditional testing and comparison of shell scripts

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

Share

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

This blog is written with reference to the document "shell programming practice" published by the old boy, which is a very well-written document.

Blog outline:

Conditional test of shell script 2, file test expression 3, string test expression 4, integer binary comparison operator 5, logical operator

Usually, all kinds of tests are carried out in various conditional structures and process control structures of bash, and then different operations are performed according to the test results, and sometimes combined with conditional statements such as if to complete the test judgment, so as to reduce the errors in running the program.

A conditional test expression usually returns "true" or "false", just as a return value of 0 after the execution of a command indicates true and a non-zero indicates false.

Conditional testing of shell scripts

In bash programming, the common syntax forms of conditional testing are as follows:

Syntax 1:test: this is a method of conditional test expression using the test command, with at least one space between the test command and "". Syntax 2: []: this is the way to conditionally test expressions through [] (single parentheses), the same as the test command, which is a more recommended method. There is at least one space between the boundary of [] and the content. Syntax 3: []]: this is the way to conditionally test expressions through [[]] (double parentheses), which is newer than test and []. There is at least one space between the boundary of [[]] and the content. Syntax 4: (): this is a method of conditional test expression through (()) (double parentheses), which is generally used in if statements. (()) (double parentheses) there is no need for spaces at both ends. The test command in Syntax 1 is the same as [] in Syntax 2. The [[]] in syntax 3 is an extended test command, and the (()) in syntax 4 is often used for calculation. I usually use syntax 2, which is more convenient. You can use wildcards and so on for pattern matching in [[]] (double parentheses), which distinguishes it from several other grammatical formats. & &, | |, >, 1] & & echo 1 | | echo 0 # looks fine. Oh, don't worry, move on to 1 [root@localhost ~] # [2].

< 1 ] && echo 1 || echo 0 #看吧,命名条件不成立,可还是输出了11[root@localhost ~]# [ 2 \< 1 ] && echo 1 || echo 0 #转义一下再看,OK了。0[root@localhost ~]# [ 2 = 1 ] && echo 1 || echo 0 #比较相等符号是正确的0[root@localhost ~]# [ 2 = 2 ] && echo 1 || echo 0 #输出正确1[root@localhost ~]# [ 2 != 2 ] && echo 1 || echo 0 #输出正确0 建议在使用中,还是老老实实的对照上表来使用吧,别给自己找麻烦。对于生产环境中的整数比较,推荐使用[ ](类似-eq的用法)。 关于[ ]、[[ ]]、(( ))用法总结: 整数加双引号的比较是对的。[[ ]]中用类似-eq的写法是对的,[[ ]]中用类似>

、 、 、

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