Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell script-for Loop

Shulou Source: shulou.com Published: 2022-06-03 00:08:46 10月02日 Update

Loop statement

For loop statement

You need to specify a variable and a list of values that can be taken, and execute the same command sequence for each different value until the variable value is exhausted and exit the loop

Value list: the execution condition of a for statement, including multiple objects with the same properties, that need to be specified in advance.

Format: for variable name in value list do command sequence done

Note:

The Operand of the for statement is a variable with a name specified by the user

Set a list of values for the variable in advance through the in keyword

Multiple values are separated by spaces

There is a cycle between do~done.

Application areas:

1. List of string values

#! / bin/bash

# string value list

For An in computer, car, mobile phone, schoolbag do echo "I want to buy $A" done

I created a shell script named for01.sh under the / opt directory in the root directory (figure below)

In vi, it is a kind of creating script.

A represents a custom variable name

The computer car schoolbag indicates the value to be taken.

$A means to get the values in turn

Save exit

Chmod + x for01.sh adds execution permissions to scripts

2. File value list

#! / bin/bash

# File value list (command execution value list)

I=1for USER in $(cat / opt/a) do echo "$I user: $USER" let i++done

First create an a file in the / opt directory to write some names

Then create a for02.sh file to write to the following figure

$(cat / opt/a) indicates that you want to take values from this directory

Save exit to add execution permissions to the file

The effect is as follows

3. Scan the ip address

#! / bin/bash# scan ip address for IP in 192.168.108. {1.. 254} / / IP address do ping-c 3-I 0.2-W 1$ IP & > / dev/null if [$?-eq 0]; then echo "$IP is updated address!" Else echo "$IP is downloads created!" Fidone

Create a for03 directory

The figure below is as follows

192.168.0 in this. It is your own network segment.

After adding the execution permission, the result is as follows

4. Add users in batch

Method 1: people with the same last name

#! / bin/bash# add user for An in teacher {1.. 5} / / add five users do useradd $An echo 123 | passwd-- stdin $A & > / dev/null # echo 123 is to set the password done for the user

Create for04

Add execution permissions and run as shown below

Then cat / etc/passwd will display the user

Method 2: different names

Vi b.txtabcdemarcdestructureBingray Bash # batch add user for An in $(cat / opt/b) do useradd $An echo 123 | passwd-- stdin $A & > / dev/nulldone

Create a for05 as shown below

Add execution permissions to the file and run

Check it in cat / etc/passwd after completion.

Tags: Users files variables permissions loops directories statements scripts commands different same between schoolbags addresses multiple characters strings objects sequences methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn MySQL Microsoft Docker