zoukankan      html  css  js  c++  java
  • Shell编程 之 for 循环

    1. 语法结构

         

         

    2. 案例

      2.1 批量解压缩

    #!/bin/bash
    
    cd /root/test/
    ls *.tar.gz > ls.log
    ls *.tgz >> ls.log
    
    for i in $( cat ls.log )
            do
                    tar -zxf $i &> /dev/null
            done
    rm -rf ls.log
    ~                                                                           
    ~                                                                           
    ~                                                                                                                                                   
    "for2.sh" 11L, 145C
    

      2.2 批量添加指定数量的用户

    #!/bin/bash
    
    read -p "input username: " -t 30 name
    read -p "input total No. of users: " -t 30 num
    read -p "input password for users: " -t 30 psw
    
    if [ ! -z "$name" -a ! -z "$num" -a ! -z "$psw" ]
            then
            y=$( echo $num | sed 's/[0-9]//g' )
                    if [ -z "$y" ]
                    then
                    for (( i=1;i<=$num;i=i+1 ))
                            do
                                    /usr/sbin/useradd $name$i &> /dev/null
                                            echo $pass | /usr/bin/passwd --stdin $name$i &> /dev/null
                            done
                    fi
    fi
    
    ~                                                                                  
    ~                                                                                  
    ~                                                                                                                                                                 
    "for4.sh" 19L, 422C

      2.3 批量删除所有的普通用户 

    #!/bin/bash
    
    usr=$(cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f1)
    
    for i in $usr
            do
                    userdel -r $i
            done
    
    ~                                                                                  
    ~                                                                                  
    ~                                                                                                                                                                  
    "for5.sh" 9L, 127C
    

      

  • 相关阅读:
    浏览器版本过低
    虚拟PWN初探
    elasticsearch常用查询
    python安装pip模块
    spark-kafka-es交互 优化
    scala写文件
    python unittest
    scala collection(集合)
    spark-kafka-es交互
    scala语法
  • 原文地址:https://www.cnblogs.com/wnzhong/p/6391121.html
Copyright © 2011-2022 走看看