1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;在线的主机使用绿色显示;不在线的主使用红色显示;
#!/bin/bash
#
for((i=1;i<=254;i++));do
site="172.16.250.${i}"
ping -w1 -c1 $site &> /dev/null
if [[ "$?" == "0" ]];then
echo -e "\033[32m${site}\033[0m"
else
echo -e "\033[35m${site}\033[0m"
fi
done
2.写一个脚本,完成以下功能
(1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;
(2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;
(3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;
(4) 分别统计S开头和K开头的文件各有多少;
#!/bin/bash
#
for i in $(ls /etc/rc.d/rc3.d/);do
if [[ "$i" =~ ^S ]];then
S=$(echo $i | wc -l)
let Ssum+=$S
echo ${i}start
fi
if [[ "$i" =~ ^K ]];then
K=$(echo $i | wc -l)
let Ksum+=$K
echo ${i}stop
fi
done
echo "S Begin:$Ssum"
echo "K Begin:$Ksum"
3、写一个脚本,完成以下功能
(1) 脚本能接受用户名作为参数;
(2) 计算此些用户的ID之和;
#!/bin/bash
#
if [ $# -eq 0 ];then
echo "At least one parameter,try the script again,please!"
exit 1
fi
for i in $*;do
id $i &> /dev/null && uid=$(grep -E "^${i}" /etc/passwd | cut -d: -f3) || echo "This user isn't existing"
let sum+=$uid
done
echo "ID_SUM is $sum"
4、写一个脚本
(1) 传递一些目录给此脚本;
(2) 逐个显示每个目录的所有一级文件或子目录的内容类型;
(3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;
#!/bin/bash # if [ $# -eq 0 ];then echo "At least one parameter,try the script again,please!" exit 1 fi for i in $*;do if [ -d $i ];then ls $i dnum=$(ls $i | wc -l) echo "The Directory has $dnum files" else ls $i echo "This is a commom file:$i" fi done
5、写一个脚本
通过命令行传递一个参数给脚本,参数为用户名
如果用户的id号大于等于500,则显示此用户为普通用户;
#!/bin/bash
#
if [ $# -eq 0 ];then
echo "At least one parameter,try the script again,please!"
exit 1
fi
for i in $*;do
id $i &> /dev/null && uid=$(grep -E "^${i}" /etc/passwd | cut -d: -f3) || echo "This user isn't existing"
if [[ "$uid" > "500" ]];then
echo "$i is a common user"
fi
done
6、写一个脚本
(1) 添加10用户user1-user10;密码同用户名;
(2) 用户不存在时才添加;存在时则跳过;
(3) 最后显示本次共添加了多少用户;
#!/bin/bash
#
x=0
for i in {1..10};do
id user$i &> /dev/null && continue || useradd user$i && echo "user$i" | passwd --stdin user$i && let x++
done
echo "add $x users"
7、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来;
与第一题相似,就不写啦~
8、打印九九乘法表;
#!/bin/bash # for((i=1;i<=9;i++));do for((j=1;j<=i;j++));do echo -e -n "$j*$i=$[$i*$j]\t" done echo "" done
原创文章,作者:N24_涩味,如若转载,请注明出处:http://www.178linux.com/63855


评论列表(1条)
赞~~从几个脚本来看,掌握的非常不错~~继续加油~