目录
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
doread -p "Please input lucky number:" dateif [ $date = $num ]thenecho "yes !!"breakelif [ "$date" = "exit" ]thenecho "bye!"exitelif [ $date -gt $num ]thenecho "too big"elseecho "too small"fi
done
2.批量创建用户
执行users_create.sh userlist passlist
建立userlist中的用户
设定userlist中的密码为paslist中的密码
当脚本后面跟得文件个数不足两个时报错
当文件行数不一致时报错
当文件不存在时报错
当用户存在时报错
#!/bin/bash
if [ "$#" -lt "2" ]
thenecho "error: 文件不足两个"
elif [ ! -e "$1" ]
thenecho "error: $1 文件不存在"
elif [ ! -e "$2" ]
thenecho "error: $2 文件不存在"
elif [ "`awk 'END{print NR}' $1 `" != "`awk 'END{print NR}' $2 `" ]
thenecho "error: 文件行数不一致"elseLINES=`awk 'END{print NR}' $1 `for LINE in `seq 1 $LINES`doUSERNAME=`sed -n ${LINE}p $1 `PASSWORD=`sed -n ${LINE}p $2 `if id "$USERNAME" &> /dev/nullthenecho "error: $USERNAME 该用户已存在"elseuseradd $USERNAMEecho $PASSWORD | passwd --stdin $USERNAME &> /dev/nullecho "$USERNAME $PASSWORD 用户创建成功"fidone
fi
3.监控网卡Receive Transmit 数据的变化
#!/bin/bash
if [ -z "$1" ]
thenecho Please input net device following $0 !!
elif ifconfig $1 &> /dev/null
thenwhile truedoRS1=$(awk "/$1/{print \$2}" /proc/net/dev)TR1=$(awk "/$1/{print \$10}" /proc/net/dev)sleep 1RS2=$(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
elseecho no
fi
4.部署Linux
1.修改IP
2.添加本机解析
3.修改主机名
#!/bin/bash
if [ -z "1" ]
thenecho Please input ID following $0 !!
elif echo $1 | grep -E '^[[:digit:]]{1,}$'
thenecho Please input A number following $0 !!hostnamectl set-hostname vm$1echo 192.168.67.$1 vm$1 >> /etc/hostsDEV_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}'`doread -p "Do you want set $DEVICE: " STATEcase $STATE inyes|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);;*)esacnmcli connection reloadnmcli connection up $DEVICEdone
}
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" ]
thenecho "error: please input device for swap"
elif [ "$USER" != "root" ]
thenecho "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
EOFfdisk $1 <<EOF
t82
wq
EOFDISK=`fdisk -l $1 | awk 'END{print $1}'`
mkswap $DISKgrep ^$DISK /etc/fstab &&{swapon -a
} || {echo "$DISK swap swap defaults 0 0 " >> /etc/fstabswapon -a
}
7.数据库脚本
执行db_dump.sh westos(数据库密码)
脚本执行后会备份数据库中所有库到 /mnt/mysqldump目录中
备份文件的名称为库名称.sql 当此文件存在时进行如下判断
输入S跳过备份
输入B备份 库名称.sql 到库名称_backup.sql
输入O时,覆盖源文件
#!/bin/bash
if [ "$USER" != 'root' ]
thenecho "Please run $0 with root"
elif [ -z "$1" ]
thenecho "Error : please input db password following $0"
elif [ -z "`rpm -qa | grep mariadb-server`" ]
thenecho "db server is not installed"
elif [ ! -e "/var/lib/mysql/mysql.sock" ]
thenecho "db server is not runnig"
elseDB_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}
fimkdir -p /mnt/mysqldumpfor DB_NAME in $DB_LIST
doif [ ! -e "/mnt/mysqldump/${DB_NAME}.sql" ]thenmysqldump -uroot -p$1 $DB_NAME > /mnt/mysqldump/${DB_NAME}.sql && echo ${DB_NAME} is backup to /mnt/mysqldump/${DB_NAME}.sqlelseecho "/mnt/mysqldump/${DB_NAME}.sql is exist"echo "you can do this action: [S]kip [B]ackup [O]verwriter"read -p "please input action:" ACTIONcase $ACTION ins|S|Skip|SKIP);;B|b|Backup|BACKUP)mv /mnt/mysqldump/${DB_NAME}.sql /mnt/mysqldump/${DB_NAME}_backup.sqlmysqldump -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 !!"esacfi
done