The difference between for and while
The difference between for and while
The difference between row reading
#!/bin/bash
df -hl |awk 'int($5) >30' > df.txt
result=$(df -hl |awk 'int($5) >30')
echo "-------------- for遍历变量 ---------------------"
for i in $result
do
echo $i
done
echo "--------------- while 遍历遍量 ------------------------------"
echo $result |while read line
do
echo $line
done
echo "-------------- for 遍历文件 --------------------"
line=$(cat ./df.txt)
for i in line
do
echo $line
done
echo "-------------- while 遍历文件 -------------------"
while read line
do
echo $line
done <df.txt
The result of executing the above script is:
-
while loop: read the file by line, the default separator is space or Tab;
-
for loop: Read the file with spaces, that is, when you encounter a space, the loop body will be executed, so if you need to read in a line, you must convert the space into other characters.
while loop through files
for loop to traverse variables
Use different loops for different scenarios
Create user script from file
假设文件是ip.txt
maomao 123
zhuzhu 456
niuniu 789
If there are blank lines in the file, you need to use forCustom separator
#!/bin/bash
#v1.0 by stanZ 2020-12-15
if [ $# -eq 0 ];then
echo "usage:`basename $0` file"
exit 1
fi
if [ ! -f $1 ];then
echo "error file"
exit 2
fi
#希望for处理文件按回车分隔,而不是空格或者tab空格
#重新定义分隔符
#IFS内部字段分隔符
#IFS=$'\n'
IFS='
'
for line in `cat $1`
do
user=`echo "$line" |awk '{print $1}'`
passwd=`echo "$line" |awk '{print $2}'`
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user already exists"
else
useradd $user
echo "$passwd" |passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is created"
fi
fi
done
Use while
#!/bin/bash
#while create user
#v1.0 by stanZ 2020-12-17
if [ $# -eq 0 ];then
echo "usage:`basename $0` file"
exit 10
fi
if [ ! -f $1 ];then
echo "error file"
exit 5
fi
while read line
do
user=`echo $line |awk '{print $1}'`
pass=`echo $line |awk '{print $2}'`
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user already exists"
else
useradd $user
echo "$pass" |passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is created."
fi
fi
done < $1
In summary, while processing files is more convenient than for
Scripts that count various states in the system
Count the number of various states of shells in /etc/passwd
#!/bin/bash
# 首先使用for
declare -A shells
shell=`cat /etc/passwd |awk -F: '{print $NF}'`
for i in $shell
do
let shells[$i]++
done
for i in ${!shells[*]}
do
echo "$i:${shells[$i]}"
done
# 执行脚本
[root@maomao 1.11]# bash shells1.sh
/sbin/nologin:25
/bin/sync:1
/bin/bash:12
/sbin/shutdown:1
/sbin/halt:1
#!/bin/bash
# 使用while
declare -A shells
while read line
do
shell=`echo $line |awk -F: '{print $NF}'`
let shells[$shell]++
done </etc/passwd
for i in ${!shells[*]}
do
echo "$i:${shells[$i]}"
done
# 执行脚本
[root@maomao 1.11]# bash shells1.sh
/sbin/nologin:25
/bin/sync:1
/bin/bash:12
/sbin/shutdown:1
/sbin/halt:1
Count the number of TCP connection states
#!/bin/bash
# count tcp status
# by stanZ 1.3
while :
do
unset status # 取消一次变量 不然数值会重叠
declare -A status
type=$(ss -an|grep :80 |awk '{print $2}')
for i in $type
do
let status[$i]++
done
for j in ${!status[@]}
do
echo "$j:${status[$j]}"
done
sleep 1
clear
done