用户建立脚本
#### 脚本需求如下:
- 执行users_create.sh userlist passlist
- 建立userlist列表中的用户
- 设定userlist列表中的密码为passlist列表中的密码
- 当脚本后面跟的文件个数不足两时,报错
- 当文件行数不一致时报错
- 当文件不存在时报错
- 当用户存在时报错
[root@westos137 mnt]# cat users_create.sh
#!/bin/bash
if [ "$#" -lt "2" ]
then
echo -e "\033[31mPlease input userfile or passfile following script! \033[0m"
exit
elif [ ! -e "$1" ]
then
echo -e "\033[31m$1 is not exist!\033[0m"
exit
elif [ ! -e "$2" ]
then
echo -e "\033[31m$2 is not exist!\033[0m"
exit
elif [ "`sed -n $= $1`" != "`sed -n $= $2`" ]
then
echo -e "\033[31m$1's line is different then $2!!\033[0m"
exit
else
LINE=1
for USERNAME in `cat $1`
do
id $USERNAME &> /dev/null &&{
echo -e "\033[31m$USERNAME is exist!\033[0m"
}||{
PASSWD=`sed -n ${LINE}p $2`
useradd $USERNAME
echo $PASSWD | passwd --stdin $USERNAME &> /dev/null && echo -e "\033[32m$USERNAME is create !!\033[0m"
}
((LINE++))
done
fi
数据库脚本:
#### 脚本需求如下:
• 执行db_dump.sh westos(数据库密码)
• 脚本执行后会备份数据库中的所有库到/mnt/mysqldump目录中
• 备份文件名称为 “库名称.sql”当此文件存在时报错并询问动作
输入“S”跳过备份,
当输入“B"时备份“库名称.sql”文件为“库名称_backup.sql”,
当输入“O”时,覆盖源文件
[root@westos137 mnt]# cat db_dump.sh
#!/bin/bash
if [ -z "$1" ]
then
echo "Please input password for DB"
elif [ -z "`rpm -qa | grep mariadb-server`" ]
then
echo "Mariadb is not installed"
elif [ ! -e "/var/lib/mysql/mysql.sock" ]
then
echo "Mariadb is not running"
else
DB_LIST=`myaql -uroot -p$1 -NE -e "SHOW DATABASES;" 2> /dev/null | grep -E '^\*|_schema$' -v`
[ "$?" != "0" ] && {
echo "$1 is wrong password!!"
}||{
[ ! -d "/mnt/mysqldump" ] && mkdir -p /mnt/mysqldump
for DBNAME in `echo $DB_LIST`
do
[ ! -e "/mnt/mysqldump/$DBNAME.sql" ] && {
mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql && echo $DBNAME is backup!
}||{
echo "$DBNAME is exist you can [B]ackup | [O]verwirte | [S]kip"
read -p "Please input action : " ACTION
case $ACTION in
b)
mv /mnt/mysqldump/$DBNAME.sql /mnt/mysqldump/${DBNAME}_backup.sql
mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql && echo $DBNAME is backup!
;;
o)
mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql && echo $DBNAME is backup!
;;
s)
esac
}
done
}
fi
系统性能检测脚本:
#### 脚本需求如下:
• 执行check_upload.sh:
显示效果如下
cpu: 实际用量%
mem: 实际用量%
[root@westos137 mnt]# cat check_upload.sh
#!/bin/bash
ps ax -o %mem|awk 'BEGIN{N=0}!/MEM|0.0/{N+=$1}END{print "mem: "N"%"}'
ps ax -o %cpu|awk 'BEGIN{N=0}!/MEM|0.0/{N+=$1}END{print "cpu: "N"%"}'
用脚本在当前系统中添加一个swap分区
swap分区大小为500M并开机自动激活此分区
[root@westos137 mnt]# cat swap.sh
#!/bin/bash
[ -z "$*" ] && {
echo please input device!
exit
}
[ ! -e "$*" ] && {
echo "$* is not exist!"
exit
}
/usr/bin/expect <<EOF
spawn fdisk $*
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_NUM=`fdisk -l $* | tail -n 1 | awk '{print $1}' | awk -F "/dev/vdb" '{print $2}'`
DEVICE_TYPE=`fdisk -l $* | tail -n 1 | awk '{print $6}'`
if [ "$DEVICE_TYPE" != "82" ]
then
/usr/bin/expect <<EOF
spawn fdisk $*
expect {
"Command" { send "t\r";exp_continue }
"Partition number" { send "$DEVICE_NUM\r";exp_continue }
"Hex code" { send "82\r" }
}
expect "Command"
send "wq\r"
expect eof
}
EOF
fi
DEVICE=`fdisk -l /dev/vdb | tail -n 1 | awk '{print $1}'`
mkswap $DEVICE
echo "$DEVICE swap swap defaults 0 0" >> /etc/fstab
swapon -a