linux学习笔记-Shell(七):shell脚本练习

用户建立脚本

执行users_create.sh userlist passlist
建立userlist中的用户
设定userlist中的密码为paslist中的密码
当脚本后面跟得文件个数不足两个时报错
当文件行数不一致时报错
当文件不存在时报错
当用户存在时报错

#!/bin/bash
if [ "$#" -lt "2" ]
then
   echo "usage: $0 <userlist> <passwordlist> "
elif [ ! -e "$1" ]
then
   echo "error: $1 is not exist"
elif [ ! -e "$2" ]
then
   echo "error: $2 is not exist"
elif [ "`awk 'END{print NR}' $1`" != "`sed -n '$=' $2`" ]
then
   echo "$1's line is different then  $2"
else
   for Line_num in `seq 1 $(sed -n '$=' $1)`
   do
       USERNAME=`sed -n ${Line_num}p $1`
       PASSWORD=`sed -n ${Line_num}p $2`
       id $USERNAME &> /dev/null && {
         echo $USERNAME is exist

       } || {

         useradd $USERNAME
         echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null && echo $USERNAME is created!!
       }
       done
fi

数据库脚本

执行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
                 

系统性能检测脚本

执行check_upload.sh
显示效果如下:
cpu 实际用量%
mem 实际用量%

   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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值