首页 > 系统服务 > 详细

Shell循环语句

时间:2019-11-18 19:42:43      阅读:68      评论:0      收藏:0      [点我收藏+]

toc

  • 循环通用命令
    • exit 退出整个程序
    • break 结束当前循环,或跳出本层循环
    • continue 忽略本次循环剩余的代码,直接进行下一次循环

For循环语句

for 变量名 in [ 取值列表 ]
do
    循环体
done

For 循环通过 user.txt 文件批量创建用户

#!/bin/bash
for i in $(cat user.txt)
do
    id $i &>/dev/null
    if [ $? -eq 0 ];then
        echo "user $username is already exists"
    else
        useradd $i &&         echo "123.com" |passwd --stdin $i &>/dev/null
        echo "$i Is OK"
    fi
done

While循环语句

## 当条件测试成立(条件测试为真),执行循环体
while 条件测试
do
    循环体
done

While 循环通过 user.txt 文件中用户名和密码批量创建用户和添加密码

#!/bin/bash
while read user
do
    username=$(echo $user|awk ‘{print $1}‘)
    password=$(echo $user|awk ‘{print $2}‘)
    id $username &>/dev/null
    if [ $? -eq 0 ];then
        echo "user $username is already exists"
    else
        useradd $username 
        if [ $? -eq 0 ];then
        echo "$password"|passwd --stdin $username &>/dev/null
        echo "useradd $username is Created Passwd Is Ok."
        fi
    fi
done<user.txt

Shell循环语句

原文:https://www.cnblogs.com/songguoyou/p/11884266.html

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