shell应用示例

用户建立脚本

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

#!/bin/bash

if [ "$#" -lt "2" ]
then
        echo "Please input two files !!"
elif [ ! -e "$1" ]
then
        echo "$1 is not exist !!"
elif [ ! -e "$2" ]
then
        echo "$2 is not exist !!"
elif [ "`awk 'END{print NR}' $1`" != "`awk 'END{print NR}' $2`" ]
then
        echo "$1 lines is different from $2"
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`
                id $USERNAME &> /dev/null &&{
                        echo $USERNAME is exist !!
                }||{
                useradd $USERNAME
                echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null &&{
                echo $USERNAME created successfully
                }
                }
        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 <> /etc/fstab
     swapon -a
}

你可能感兴趣的:(Linux-shell,linux,运维,服务器)