1、计算当前cpu总的占用情况
代码:
echo `ps ax -o %cpu | awk 'BEGIN{N=0}!/0.0|CPU/{N+=$1}END{print N}'`%
测试:
2、倒计时脚本
脚本代码:
#!/bin/bash
read -p "Please input hour: " HR ##小时
read -p "Please input minute: " MIN ##分钟
read -p "please input sec: " SEC ##秒
for(( i=$HR*3600+$MIN*60+$SEC;i>=0;i-- ))
do
clear
echo count down $[$i/3600]:$[$i%3600/60]:$[$i%3600%60] ##时:分:秒
sleep 1 ##每隔1秒刷新1次
done
测试:1小时30分钟20秒的倒计时
3、幸运数字游戏
脚本代码:
#!/bin/bash
N=$[$RANDOM%10] ##生成随机数
while true
do
read -p "Please input luck number:" NUMBER
if [ "$NUMBER" = "exit" ]
then
exit
elif [ "$NUMBER" -lt "$N" ]
then
echo $NUMBER is too small
elif [ "$NUMBER" -gt "$N" ]
then
echo $NUMBER is too large
elif [ "$NUMBER" -eq "$N" ]
then
echo $NUMBER is luck number!!
exit
fi
done
测试:
4、用户建立脚本
脚本需求如下:
• 执行users_create.sh userlist passlist
• 建立userlist列表中的用户
• 设定userlist列表中的密码为passlist列表中的密码
• 当脚本后面跟的文件个数不足两时,报错
• 当文件行数不一致时报错
• 当文件不存在时报错
• 当用户存在时报错
#!/bin/bash
if [ "$#" -lt "2" ] ##所跟文件数目不足2
then
echo Error:please input userfile and passwordfile
exit
elif [ ! -e "$1" ]
then
echo Error:$1 is not exist
exit
elif [ ! -e "$2" ]
then
echo Error:$2 is not exist
exit
else
[ `cat $1 | wc -l` -ne `cat $2 | wc -l` ] && {
echo "Error:$1's line is different then $2"
exit
} || {
for NUM in `seq 1 $(cat $1 | wc -l)`
do
NAME=`sed -n "$NUM"p $1` ##抓取用户名
PASSWD=`sed -n "$NUM"p $2` ##抓取密码
id $NAME &> /dev/null && {
echo $NAME is exist
} || {
useradd $NAME
echo $PASSWD | passwd --stdin $NAME &> /dev/null && echo $NAME is create
}
done
}
fi
测试:
5、系统性能检测脚本
执行check_upload.sh
显示效果如下:
cpu: 实际用量%
mem: 实际用量%
脚本代码:
#!/bin/bash
echo cpu: `ps ax -o %cpu | awk 'BEGIN{N=0}!/0.0|CPU/{N+=$1}END{print N}'`%
echo mem: `ps ax -o %mem | awk 'BEGIN{N=0}!/0.0|MEM/{N+=$1}END{print N}'`%
测试:
6、DNS_A.sh脚本
检测172.25.254.1-10是否可以通信,可通信的将解析列表保存到/mnt/hostlist
格式为:
172.25.254.1 主机名
脚本代码:
#!/bin/bash
SSH()
{
/usr/bin/expect <<EOF
spawn ssh -l root $1 hostname
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "westos\r" }
}
expect eof
EOF
}
for IP in `seq 1 10` ##172.25.254.1-10
do
ping -c1 -w1 172.25.254.$IP &> /dev/null && {
touch /mnt/hostlist
grep 172.25.254.$IP /mnt/hostlist &> /dev/null ##检查解析列表中是否存在当前ip的解析
if [ $? -ne 0 ] ##退出值不等于0,则上一个指令不成功。即无当前ip的解析
then ##将解析存到文件
echo "172.25.254.$IP $(SSH 172.25.254.$IP | tail -n 1)" >> /mnt/hostlist
fi
} || {
echo "172.25.254.$IP is down"
}
done
测试:172.25.254.1-10 只有5可以通信
当解析文件里存在172.25.254.5的解析时,不在继续存入
7、用脚本在当前系统中添加一个swap分区,swap分区大小为500M并开机自动激活此分区
脚本代码:
#!/bin/bash
/usr/bin/expect &> /dev/null <<EOF
spawn fdisk $1
expect {
"Command" { send "n\r";exp_continue }
"Select" { send "p\r";exp_continue }
"Partition number" { send "\r";exp_continue }
"First sector" { send "\r";exp_continue }
"Last sector" { send "+500M\r" }
}
expect {
"signature?" { send "yes\r";exp_continue }
"Command" { send "wq\r" }
}
expect eof
EOF
DEVICE_NAME=`fdisk -l $1 | tail -n 1 | awk '{print $1}'` ##抓取新添加的设备名称
DEVICE_NUM=`fdisk -l $1 | tail -n 1 | awk '{print $1}' | awk -F "$1" '{print $2}'` ##抓取新添加设备的分区数字
DEVICE_TYPE=`fdisk -l $1 | tail -n 1 | awk '{print $6}'` ##抓取新添加设备的类型
if [ $DEVICE_TYPE != 82 ]
then
/usr/bin/expect &> /dev/null <<EOF
spawn fdisk $1
expect {
"Command" { send "t\r";exp_continue }
"Partition number" { send "$DEVICE_NUM\r";exp_continue }
"Hex" { send "82\r" }
}
expect {
"Command" { send "wq\r" }
}
expect eof
EOF
fi
mkswap $DEVICE_NAME &> /dev/null
grep $DEVICE_NAME /etc/fstab &> /dev/null
if [ $? -ne "0" ]
then
echo "$DEVICE_NAME swap swap defaults 0 0" >> /etc/fstab
fi
测试: