In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
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.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.