首页 > 系统服务 > 详细

Shell编程 之 for 循环

时间:2017-02-12 17:22:20      阅读:239      评论:0      收藏:0      [点我收藏+]

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

  

 

Shell编程 之 for 循环

原文:http://www.cnblogs.com/wnzhong/p/6391121.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!