shell脚本练习之while
2020-07-29 20:22
615 查看
刚开始学shell?
那就肯定要学习while循环,
把下面几个练习做出来,你就学会while循环了!
- 使用while循环,添加user1-user10,分别设置密码为pass1-pass10,不添加user5
#!/bin/bash i=0 while [ $i -le 10 ]; do let i++ if [ $i -eq 5 ];then continue fi useradd user${i} 1> /dev/null echo pass${i}|passwd --stdin user${i} > /dev/null done
- 使用while循环,将上题中的9个用户删除,删除成功给出如下提示“user xxx delete successfully.”
#!/bin/bash i=1 while [ $i -le 10 ]; do if [ $i -eq 5 ];then let i++ continue fi userdel -r user${i} 1> /dev/null echo "user${i} delete successfully." let i++ done
- 使用while循环计算1+2+3+…+100
#!/bin/bash i=1 sum=0 while [ $i -le 100 ]; do let sum+=i let i++ done echo $sum
- 使用while循环打印出所有用户信息,格式如下:用户名:XXX,UID:XXX,GID:XXX
while read line do username=`echo ${line} | awk -F: '{print $1}'` userID=`echo ${line} | awk -F: '{print $3}'` userGID=`echo ${line} | awk -F: '{print $4}'` echo "Username: $username UserID: $userID UserGID: $userGID" done < /etc/passwd
- 使用while循环,打印出passwd文件所有的奇数行行号及内容。
#!/bin/bash i=1 while read odd_line do [ $(( ${i}%2 )) != 0 ] && echo "${i} ${odd_line}" let i++ done < /etc/passwd
<以上就是while的两种用法,代码仅供参考>
相关文章推荐
- shell脚本练习2 (while循环语句)
- shell脚本练习一(if语句、case语句、for语句、while语句)
- shell脚本 练习16------20
- Shell脚本编程之While循环
- shell脚本练习003
- shell 脚本之 while 循环
- Shell脚本文件练习
- 在shell脚本中用while写猜数字游戏
- shell脚本练习
- 2个简单shell脚本(if,while,case语句)
- 简单shell脚本练习
- shell小练习:编写脚本检测192.168.1.0/24网段中在线的主机,将在线的主机和不在线主机的IP地址分别记录在不同的文件中。
- shell脚本练习(12.10)
- Shell脚本基础6-if语句练习-文件测试操作
- shell脚本练习
- SHELL训练营--day9__告警脚本练习
- 【shell脚本练习】判断目录读写权限
- shell脚本练习
- shell 脚本练习
- shell脚本小练习--域名解析