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 realize flow Control in Shell

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

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you how to achieve process control in Shell, I hope you have something to gain after reading this article, let's discuss it together!

Shell process control

In Linux programming, flow control statements are basically control statements of if, for, while, until, case and other conditions.

If control

Common grammatical structures of if

If (expression); then echo .else echo .fi

Demo:

#! / bin/bashnum=10if ((${num} > 4)); thenecho "${num} greater than 3" fiif test [${num} > 4]; thenecho "${num} greater than 4" fiif multiple judgments #! / bin/bashnum=10;if [$num-lt 8]; thenecho "${num} less than 8" elif [${num}-eq 10]; thenecho "${num} equals 10" elif [${num}-gt 11] Then echo "${num} > 10" else echo "does not meet the condition" fi;for loop

For loop syntax structure

For variable in string do echo "" done

Demo:

#! / bin/bash# defines an array val= (1 2 3 4 56) for i in ${val [*]} # or you can directly in `seq 6`do echo "this is num: $I" done

Output result:

# print result: this is num: 1this is num: 2this is num: 3this is num: 4this is num: 5this is num: 6

Demo 2:

#! / bin/bash # batch package for i in `find / var/log-name "* .log" `do tar-czf 2019log.tgz $I donewhile loop for lookup files

While grammatical structure

While (conditional judgment) do echo "" done

Demo:

#! / bin/sh iTunes 1; while ($I

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