Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to realize flow Control in Shell

Shulou Source: shulou.com Published: 2022-05-31 12:34:04 09月21日 Update

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

Tags: Loop presentation control number input file process condition statement structure result syntax service area article service output selection between content variable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Microsoft Docker OPPO Reno MySQL