Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is the structure of for loop in Shell programming

Shulou Source: shulou.com Published: 2022-05-31 12:33:51 10月03日 Update

Editor to share with you what the structure of the for loop in Shell programming is. I hope you will get something after reading this article. Let's discuss it together.

For i in {value range}

Do

Cyclic body

Done

Find the sum of the numbers from 1 to 100, the numbers divisible by 2 from 1 to 100, the primes from 1 to 100, and the numbers divisible by 3 from 1 to 100.

#! / usr/bin/env bash

#

# Author: bavdu

# Email: bavduer@163.com

# Github: https://github.com/bavdu

# Date: 2019 Compact

Sum=0

For i in {1..100}

Do

Let total=$sum+$i # or write as let total=i++

Done

Printf "$total\ n"

Test host viability in production environment

#! / usr/bin/env bash

#

# Author: bavdu

# Email: bavduer@163.com

# Github: https://github.com/bavdu

# Date: 2019 Compact

> ip_alive.txt

> ip_down.txt

Export segment= "192.168.161"

For i in {2..254}

Do

{

Ping-C1 $segment.$i & > / dev/null

If [$?-eq 0]; then

Printf "alive: $segment.$i\ n" > > ip_alive.txt

Else

Printf "down: $segment.$i\ n" > > ip_down.txt

Fi

} &

Done

Wait

Printf "$(date +% Y-%m-%d_%k:%M:%S)-exec filename:$0\ n"

After reading this article, I believe you have a certain understanding of "what is the structure of the for loop in Shell programming". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Loops numbers structures programming articles hosts done loops more environment knowledge primes ranges industries information information channels channels testing production Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple OPPO Reno Huawei Microsoft