判断用户名文件的行数是否大于25,如果大于25,提示 row above 25.
[root@localhost ~]b=$(cat /etc/passwd|wc -l);if [ $b -gt 25 ];then echo "row above 25"; fi
截取系统一分钟的平均负载,只取整数部分
[root@localhost ~]uptime|awk '{printf $8}'|cut -c 1
统计连接状态 established 和 listen 的数量
[root@localhost ~]num_listen=netstat -anpt|grep "LISTEN"|wc -l
[root@localhost ~]num_established=netstat -anpt|grep "ESTABLISHED"|wc -l
[root@localhost ~]echo $num_listen
[root@localhost ~]echo $num_established
判断用户名文件是否是20行,如果是,输出ok
[root@localhost ~]test=$(cat find.sh|wc -l);if [ $test -eq 20 ];then echo "ok";fi
统计 logfile 中每个IP的访问量
[root@localhost ~]sort logfile |uniq -c
统计logfile中访问量超过15的IP
[root@localhost ~]sort logfile |uniq -c|awk '$1>=15{printf $1 "\t" $2 "\n"}'
统计所有进程占用的内存大小,并计算总和。
[root@localhost ~]ps aux|awk '{printf $1 "\t" $6 "\n"}'
[root@localhost ~]ps aux|awk '{sum+=$6} END {print "进程内存总和为",sum,"KB"}'
用户输入数字,如果输入的是非数字,提示 “Include nunumbers, retry please!”并结束,
如果是纯数字,返回数字结果。
#!/bin/bash
read -p "Please input a number:" num
test=$(echo $num|sed 's/[0-9]//g')
if [ -z $test ];then
echo $num
else
echo "Include nunumbers,retry please!"
exit 1
fi
输入一个数字,运行对应的一个命令。
#!/bin/bash
PS3="Please select a number:"
select cmd in date ls who pwd exit
do
$cmd
done
提示用户输入网卡的名字,输出网卡的ip。
#!/bin/bash
read -p "Please input network name:" net
ip=$(ifconfig $net|grep "inet"|grep -v "inet6"|awk '{print $2}')
echo "The IP address of the network is $ip"
while 循环实现每隔 10s 执行一次 w 命令
[root@localhost ~]while true;do w;sleep 10;done
实现交换两个文件的文件名
#!/bin/bash
read -p "Please input first filename:" file1
read -p "Please input second filename:" file2
mv $file1 tmp
mv $file2 $file1
mv tmp $file2
统计文件中仅包含数字的行
#!/bin/bash
read -p "Please input filename path:" file
for i in $(cat $file)
do
test=$(echo $i|sed 's/[0-9]//g')
if [ -z $test ];then
echo $i
fi
done