for 变量 in 列表;
循环体
done
如何生成列表;
{1..100}
seq [起始数][步长]结束值;
#!/bin/bash
lines=`cat /etc/passwd |wc -l`
for i in `seq 1 $lines`;
do
user=`cut -d: -f1 /etc/passwd | head -$i | tail -1`
echo "hello:$user"
done
输出shell为bash的用户
#!/bin/bash
lines=`cat /etc/passwd |wc -l`
for i in `seq 1 $lines`;
do
#user=`cut -d: -f1 /etc/passwd`
user=`cut -d: -f1 /etc/passwd | head -$i | tail -1`
shell=`cut -d: -f7 /etc/passwd | head -$i | tail -1`
if grep ‘\<bash\>‘ $shell &>/tmp/out.txt ;then
echo "hello:$user,your shel:$shell"
fi
done
添加删除十个用户
#!/bin/bash
#
if [ $1 == add ]&>/tmp/out.txt ;then
for i in {1..10};do
useradd $i
done
echo "useradd finish"
elif [ $1 == del ]&>/tmp/out.txt ;then
for i in {1..10};do
userdel -r $i
done
echo "userdell finish"
else
echo "please use add or del"
fi
时间: 2024-10-17 02:55:09