1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;
在线的主机使用绿色显示;
不在线的主使用红色显示;
#!/bin/bash
#
for i in {1..254}; do
if -W 1 -c 1 ping 172.16.250.$i &> /dev/null; then
echo -e "\033[32mHost 172.16.250.$i is online.\033[0m"
else
echo -e "\033[31mHost 172.16.250.$i is offline.\033[0m"
fi
done
2、如何给网络接口配置多个地址,有哪些方式?
~]# ip addr add 192.168.121.11/24 dev eno16777736:1 ~]# ifconfig eth0:1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
3、写一个脚本,完成以下功能
(1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;
(2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;
(3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;
(4) 分别统计S开头和K开头的文件各有多少;
#!/bin/bash
#
declare -i k=0,s=0
for i in $(ls /etc/rc.d/rc3.d/K* | grep -E -o "[^/]+$"); do
echo "$i stop"
let k++
done
for j in $(ls /etc/rc.d/rc3.d/S* | grep -E -o "[^/]+$"); do
echo "$j start"
let s++
done
echo "Filename begins with "s": $s"
echo "FIlename begins with "k": $k"
4、写一个脚本,完成以下功能
(1) 脚本能接受用户名作为参数;
(2) 计算此些用户的ID之和;
#!/bin/bash
#
declare -i sum=0
if [ $# -lt 1 ];then
echo "At least one username!"
exit 1
fi
for user in $@;do
if id $user &> /dev/null; then
sum=$[$sum+$(id -u $user)]
else
echo "No such user!"
exit 2
fi
done
echo "$sum"
5、写一个脚本
(1) 传递一些目录给此脚本;
(2) 逐个显示每个目录的所有一级文件或子目录的内容类型;
(3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;
#!/bin/bash
#
declare -i dir=0,file=0,dir_all=0,file_all=0
for i in $@;do
if [ -d $i ]; then
for j in $(ls $i);do
echo "$j"
if [ -f $i/$j ];then
let file+=$file
elif [ -d $i/$j ];then
let dir+=$dir
fi
done
fi
file_all=$[$file_all+$file]
dir_all=$[$dir_all+$dir]
done
echo "File num: $file_all"
echo "Directory num: $dir_all"
6、写一个脚本
通过命令行传递一个参数给脚本,参数为用户名
如果用户的id号大于等于500,则显示此用户为普通用户;
#!/bin/bash
#
if [ $# -eq 0 ];then
echo "At least one username!"
exit 1
fi
id=$(id -u $1)
if [ $id -ge 500 ]; then
echo "login user"
else
echo "system user"
fi
7、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来;
#!/bin/bash
#
for((i=20;i<=100;i++)); do
if ping -W 1 -c 1 172.16.250.$i &> /dev/null; then
echo "172.16.250.$i is online"
fi
done
8、打印九九乘法表;
#!/bin/bash
#
declare -i j=1
declare -i i=1
while [ $j -le 9 ];do
while [ $i -le $j ];do
echo -n -e "${i}X${j}=$[$j*$i]\t"
let i++
done
let j++
let i=1
echo
done
原创文章,作者:浙江-咲,如若转载,请注明出处:http://www.178linux.com/71784


评论列表(1条)
可以看出脚本运用的已经比较熟练了,脚本在手,天下我有,继续加油。