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

  

 

posted on 2017-02-12 15:46  你的踏板车要滑向哪里  阅读(88)  评论(0编辑  收藏  举报

导航