目录
随机数的生成
使用环境变量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
执行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
- #!/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
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
执行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}' `)%
用脚本在当前系统添加一个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
- }
执行db_dump.sh westos(数据库密码)
脚本执行后会备份数据库中所有库到 /mnt/mysqldump目录中
备份文件的名称为库名称.sql 当此文件存在时进行如下判断
输入S跳过备份
输入B备份 库名称.sql 到库名称_backup.sql
输入O时,覆盖源文件
- #!/bin/bash
- if [ -z "$1" ]
- then
- echo "Please input password for mysql !"
- exit
- fi
-
- rpm -q mariadb-server &> /dev/null || {
- dnf install mariadb-server.x86_64 -y
- }
-
- systemctl status mariadb.service | grep "running" &> /dev/null || {
- systemctl start mariadb.service &> /dev/null
- }
-
- DBS=`mysql -uroot -p$1 -EN -e "SHOW DATABASES;" 2> /dev/null | grep -vE "row|_schema$"`
- ##设定DB并且检测数据库密码是否正确 -N 去表头 2> 过滤错误输出
- if [ "$?" -ne "0" ]
- then
- echo "Error: Wrong password for db !"
- else
- mkdir -p /mnt/mysqldump ##建立多层目录
- for DBNAME in $DBS
- do
- if [ ! -e "/mnt/mysqldump/$DBNAME.sql" ] ##如果不存在
- then
- mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql ##备份
- else ##如果存在
- echo "/mnt/mysqldump/$DBNAME.sql is exist !" ##显示已存在
- echo "You can do action: [S]kip [B]ackup [O]verwrite [E]xit" ##提示下一步动作
- while true
- do
- read -p "Please input action: " ACTION
- case $ACTION in
- s|S|skip|Skip) ##直接跳过
- break
- ;;
- b|B|backup|Backup) ##将已存在的备份文件改名并重新备份
- mv /mnt/mysqldump/$DBNAME.sql /mnt/mysqldump/${DBNAME}_backup_`date +%m%d%h`.sql
- mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql
- break
- ;;
- o|O|overwrite|Overwrite) ##覆盖已存在的备份文件
- mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql
- break
- ;;
- e|E|exit|Exit) ##退出
- break
- ;;
- *)
- echo Wrong action !! ##错误指令
- continue
- esac
- done
- fi
- done
- fi
-
-