shell脚本练习题
1.查找当前网段(10.1.1.0/24)内存活IP用户,重定向到/tmp/ip.txt文件中(ping)
#!/bin/bash
ip=1
while [ $ip -lt 255 ];do
ping -c 1 -w 1 10.1.1.$ip 2>&1 > /dev/null
if [ $? -eq 0 ];then
echo "10.1.1.$ip" >> /tmp/ip.txt
fi
ip=`expr $ip + 1`
done
2.自动创建用户student101-150,且密码为password101-150
#!/bin/bash
for i in {101..150};do
id student$i > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "User student$i exist"
else
useradd student$i
echo "User studen$i added"
echo "password$i" | passwd --stdin student$i > /dev/null
fi
done
3.根据passwd文件内容,输出“The line 1(行数) is root(用户名)”依次类推
#!/bin/bash
n=1
while read line; do
user=`echo $line | awk -F: '{print $1}' `
echo "The line $n is $user"
l