• shell脚本之练习题


     设计一个shell程序,添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。

    i=1  
    groupadd class1  
    while [ $i -le 30 ]  
    do  
    if [ $i -le 9 ] ;then  
    USERNAME=stu0${i}  
    else  
    USERNAME=stu${i}  
    fi  
    useradd $USERNAME  
    chgrp -R class1 /home/$USERNAME  
    i=$(($i+1))  
    done  
    

     

     编写shell程序,实现自动删除50个账号的功能。账号名为stud1至stud50。

    #!/bin/sh
    i=1
    while [ $i -le 50 ]
    do
    userdel -r stud${i}
    i=$(($i+1 ))
    done
    

     

    mysql备份实例,自动备份mysql,并删除30天前的备份文件

    #!/bin/sh
    
    #auto backup mysql
    #wugk  2012-07-14
    #PATH DEFINE
    
    BAKDIR=/data/backup/mysql/`date +%Y-%m-%d`
    MYSQLDB=www
    MYSQLPW=backup
    MYSQLUSR=backup
    
    if[ $UID -ne 0 ];then
    echo This script must use administrator or root user ,please exit!
    sleep 2
    exit 0
    fi
    
    if[ ! -d $BAKDIR ];then
    mkdir -p $BAKDIR
    else
    echo This is $BAKDIR exists ,please exit ….
    sleep 2
    exit
    fi
    
    ###mysqldump backup mysql
    
    /usr/bin/mysqldump -u$MYSQLUSR -p$MYSQLPW -d $MYSQLDB >/data/backup/mysql/`date +%Y-%m-%d`/www_db.sql
    
    cd $BAKDIR ; tar -czf  www_mysql_db.tar.gz *.sql
    
    cd $BAKDIR ;find  . -name “*.sql” |xargs rm -rf[ $? -eq 0 ]&&echo “This `date +%Y-%m-%d` RESIN BACKUP is SUCCESS”
    
    cd /data/backup/mysql/ ;find . -mtime +30 |xargs rm -rf
    

     

    自动安装Nginx脚本,采用case方式,选择方式,也可以根据实际需求改成自己想要的脚本

    #!/bin/sh
    
    ###nginx install shell
    ###wugk 2012-07-14
    ###PATH DEFINE
    
    SOFT_PATH=/data/soft/
    NGINX_FILE=nginx-1.2.0.tar.gz
    DOWN_PATH=http://nginx.org/download/
    
    if[ $UID -ne 0 ];then
    echo This script must use administrator or root user ,please exit!
    sleep 2
    exit 0
    fi
    
    if[ ! -d $SOFT_PATH ];then
    mkdir -p $SOFT_PATH
    fi
    
    download ()
    {
    cd $SOFT_PATH ;wget $DOWN_PATH/$NGINX_FILE
    }
    
    install ()
    {
    yum install pcre-devel -y
    cd $SOFT_PATH ;tar xzf $NGINX_FILE ;cd nginx-1.2.0/ &&./configure –prefix=/usr/local/nginx/ –with-http_stub_status_module –with-http_ssl_module
    [ $? -eq 0 ]&&make &&make install
    }
    
    start ()
    {
    lsof -i :80[ $? -ne 0 ]&&/usr/local/nginx/sbin/nginx
    }
    
    stop ()
    {
    ps -ef |grep nginx |grep -v grep |awk ‘{print $2}’|xargs kill -9
    }
    
    exit ()
    {
    echo $? ;exit
    }
    
    ###case menu #####
    
    case $1 in
    download )
    download
    ;;
    
    install )
    install
    ;;
    
    start )
    start
    ;;
    stop )
    stop
    ;;
    
    * )
    
    echo “USAGE:$0 {download or install or start or stop}”
    exit
    esac
    

     

    文件上传,列出部分代码

    #!/bin/sh
    
    DATE=`date +%Y_%m_%d_%H`
    
    if [ $1 ]
    then
      for file in $(sed '/^$/d' $1)     //去掉空行
      do
        if [ -f $file ]                 //普通文件
        then
          res=`scp $file $2:$file`      //上传文件
          if [ -z $res ]                //上传成功
          then
            echo $file >> ${DATE}_upload.log   //上传成功的日志
          fi
        elif [ -d $file ]              //目录
        then
          res=`scp -r $file $2:$file`
          if [ -z $res ]
          then
            echo $file >> ${DATE}_upload.log
          fi
        fi
      done
    else
      echo "no file" >> ${DATE}_error.log
    fi
    

     

  • 相关阅读:
    Ubuntu 18.04更换国内源方法
    CTFHub-Web-Web前置技能-PHPINFO
    CTFHub-Web-Web前置技能-目录遍历
    Python-字符串常见操作
    hadoop完全分布式虚拟机多机克隆后网卡配置
    N皇后问题 回溯非递归算法 C++实现2
    N皇后问题 回溯非递归算法 C++实现1
    N皇后问题 回溯递归算法 C++实现2
    N皇后问题 回溯递归算法 C++实现1
    无法更新运行时文件夹共享状态:在客户机操作系统内装载共享文件夹文件系统时出错——解决方案
  • 原文地址:https://www.cnblogs.com/leomei91/p/7490642.html
Copyright © 2020-2023  润新知