目录
1.猜数字
2.批量创建用户
3.监控网卡Receive Transmit 数据的变化
4.部署Linux
5.系统性能检测脚本
6.分区脚本
7.数据库脚本
1.猜数字
随机数的生成
使用环境变量RANDOM
,范围是0~32767
编写guest.sh
,实现以下功能:
1、随机生成一个1~20的数字;
2、每猜一次都会提示目前数字与随机数的大小关系,直到用户猜对提示成功;
3、当用户输入exit
时,退出脚本。
#!/bin/bash
let num=$RANDOM%20
while true
do
read -p "Please input lucky number:" date
if [ $date = $num ]
then
echo "yes !!"
break
elif [ "$date" = "exit" ]
then
echo "bye!"
exit
elif [ $date -gt $num ]
then
echo "too big"
else
echo "too small"
fi
done
2.批量创建用户
执行users_create.sh userlist passlist
建立userlist中的用户
设定userlist中的密码为paslist中的密码
当脚本后面跟得文件个数不足两个时报错
当文件行数不一致时报错
当文件不存在时报错
当用户存在时报错
#!/bin/bash
if [ "$#" -lt "2" ]
then
echo "error: 文件不足两个"
elif [ ! -e "$1" ]
then
echo "error: $1 文件不存在"
elif [ ! -e "$2" ]
then
echo "error: $2 文件不存在"
elif [ "`awk 'END{print NR}' $1 `" != "`awk 'END{print NR}' $2 `" ]
then
echo "error: 文件行数不一致"
else
LINES=`awk 'END{print NR}' $1 `
for LINE in `seq 1 $LINES`
do
USERNAME=`sed -n ${LINE}p $1 `
PASSWORD=`sed -n ${LINE}p $2 `
if id "$USERNAME" &> /dev/null
then
echo "error: $USERNAME 该用户已存在"
else
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null
echo "$USERNAME $PASSWORD 用户创建成功"
fi
done
fi
3.监控网卡Receive Transmit 数据的变化
#!/bin/bash
if [ -z "$1" ]
then
echo Please input net device following $0 !!
elif ifconfig $1 &> /dev/null
then
while true
do
RS1=$(awk "/$1/{print \$2}" /proc/net/dev)
TR1=$(awk "/$1/{print \$10}" /proc/net/dev)
sleep 1
RS2=$(awk "/$1/{print \$2}" /proc/net/dev)
TR2=$(awk "/$1/{print \$10}" /proc/net/dev)
echo -ne "Receice: $[$RS2-$RS1]byte/s, Transmit: $[$TR2-$TR1]byte/s"
echo -ne "\r"
done
else
echo no
fi
4.部署Linux
1.修改IP
2.添加本机解析
3.修改主机名
#!/bin/bash
if [ -z "1" ]
then
echo Please input ID following $0 !!
elif echo $1 | grep -E '^[[:digit:]]{1,}$'
then
echo Please input A number following $0 !!
hostnamectl set-hostname vm$1
echo 192.168.67.$1 vm$1 >> /etc/hosts
DEV_COUNT=`nmcli device show | awk 'BEGIN{N=0}/GENERAL.DEVICE/&&!/lo/{N++}END{ print N}'`
[ "$DEV_COUNT" -gt "1" ]&&{
for DEVICE in `nmcli device show | awk '/GENERAL.DEVICE/&&!/lo/{ print $2}'`
do
read -p "Do you want set $DEVICE: " STATE
case $STATE in
yes|YES|y|Y)
read -p "Please input ip address: " IP
cat > /etc/sysconfig/network-scripts/ifcfg-$DEVICE <<EOF
BOOTPROTO=none
NAME=$DEVICE
DEVICE=$DEVICE
ONBOOT=yes
IPADDR=$IP
PREFIX=24
DNS1=114.114.114.114
EOF
;;
no|NO|n|N)
;;
*)
esac
nmcli connection reload
nmcli connection up $DEVICE
done
}
fi
5.系统性能检测脚本
执行check_upload.sh
显示效果如下:
cpu 实际用量%
mem 实际用量%
#!/bin/bash
echo cpu 实际用量 $(printf '%0.2f' `ps ax -o %cpu | awk 'BEGIN{CPU=0}{CPU+=$1}END{print CPU}' `)%
echo mem 实际用量 $(printf '%0.2f' `ps ax -o %mem | awk 'BEGIN{MEM=0}{MEM+=$1}END{print MEM}' `)%
6.分区脚本
用脚本在当前系统添加一个swap分区
swap分区大小为500M并开机自动激活分区
#!/bin/bash
if [ -z "$1" ]
then
echo "error: please input device for swap"
elif [ "$USER" != "root" ]
then
echo "error: please run $0 with root!!"
else
[ ! -b "$1" ] && {
echo "$1 is not block device"
exit
}
fi
/usr/bin/expect <<EOF
spawn fdisk $1
expect {
"Command" {send "n\r"}
}
expect {
"Select" { send "\r";exp_continue }
"Partition number" { send "\r";exp_continue }
"First sector" { send "\r";exp_continue }
"Last sector" { send "+500M\r";exp_continue }
"signature" { send "yes\r";exp_continue }
"Command" { send "wq\r" }
}
expect eof
EOF
fdisk $1 <<EOF
t
82
wq
EOF
DISK=`fdisk -l $1 | awk 'END{print $1}'`
mkswap $DISK
grep ^$DISK /etc/fstab &&{
swapon -a
} || {
echo "$DISK swap swap defaults 0 0 " >> /etc/fstab
swapon -a
}
7.数据库脚本
执行db_dump.sh westos(数据库密码)
脚本执行后会备份数据库中所有库到 /mnt/mysqldump目录中
备份文件的名称为库名称.sql 当此文件存在时进行如下判断
输入S跳过备份
输入B备份 库名称.sql 到库名称_backup.sql
输入O时,覆盖源文件
#!/bin/bash
if [ "$USER" != 'root' ]
then
echo "Please run $0 with root"
elif [ -z "$1" ]
then
echo "Error : please input db password following $0"
elif [ -z "`rpm -qa | grep mariadb-server`" ]
then
echo "db server is not installed"
elif [ ! -e "/var/lib/mysql/mysql.sock" ]
then
echo "db server is not runnig"
else
DB_LIST=`mysql -uroot -p$1 -NE -e "SHOW DATABASES;" 2> /dev/null |grep -E "^\*|_schema$" -v`
[ "$?" != "0" ] && {
echo "ERROR: $1 is wrong password !!"
exit 1
}
fi
mkdir -p /mnt/mysqldump
for DB_NAME in $DB_LIST
do
if [ ! -e "/mnt/mysqldump/${DB_NAME}.sql" ]
then
mysqldump -uroot -p$1 $DB_NAME > /mnt/mysqldump/${DB_NAME}.sql && echo ${DB_NAME} is backup to /mnt/mysqldump/${DB_NAME}.sql
else
echo "/mnt/mysqldump/${DB_NAME}.sql is exist"
echo "you can do this action: [S]kip [B]ackup [O]verwriter"
read -p "please input action:" ACTION
case $ACTION in
s|S|Skip|SKIP)
;;
B|b|Backup|BACKUP)
mv /mnt/mysqldump/${DB_NAME}.sql /mnt/mysqldump/${DB_NAME}_backup.sql
mysqldump -uroot -p$1 $DB_NAME > /mnt/mysqldump/${DB_NAME}.sql && echo ${DB_NAME} is backup to /mnt/mysqldump/${DB_NAME}.sql
;;
o|O|Overwriter|OVERWRITER)
mysqldump -uroot -p$1 $DB_NAME > /mnt/mysqldump/${DB_NAME}.sql && echo ${DB_NAME} is backup to /mnt/mysqldump/${DB_NAME}.sql
;;
*)
echo "error:wrong action !!"
esac
fi
done