bash脚本中如何使用while

发布时间:2021-12-20 09:21:43 作者:小新
来源:亿速云 阅读:204

这篇文章主要为大家展示了“bash脚本中如何使用while”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“bash脚本中如何使用while”这篇文章吧。

脚本编程:
 顺序结构
 选择结构
  if
  case
 循环结构
  for
  while
  until
  
while循环:适用于循环次数未知的场景,要有退出条件
语法:
 while CONDITION; do
   statement
   ...
 done
 


练习:计算100以内所有正整数的和

[root@localhost ~]# bash sum3.sh
the sum is: 5050
[root@localhost ~]# cat sum3.sh
#!/bin/bash

declare -i i=1
declare -i sum=0

while [ $i -le 100 ]; do
 let sum+=$i
 let i++
done

echo "the sum is: $sum"
[root@localhost ~]#

将小写转换为大写

[root@localhost ~]# bash translate.sh
input string:asdf
ASDF
input string:bad
BAD
input string:quit
[root@localhost ~]# cat translate.sh
#!/bin/bash

read -p "input string:" string

while [ $string != 'quit' ]; do
 echo $string | tr 'a-z' 'A-Z'
 read -p "input string:" string
done

[root@localhost ~]#

练习:每隔5秒查看hadoop用户是否登录,如果登录,显示其登录并退出;否则,显示当前时间,并说明hadoop尚未登录:

[root@localhost ~]# vim checkhadoop.sh
[root@localhost ~]# bash +x checkhadoop.sh
Thu Feb 23 20:53:33 PST 2017
Thu Feb 23 20:53:38 PST 2017
Thu Feb 23 20:53:43 PST 2017
Thu Feb 23 20:53:48 PST 2017
Thu Feb 23 20:53:53 PST 2017
Thu Feb 23 20:53:58 PST 2017
Thu Feb 23 20:54:03 PST 2017
Thu Feb 23 20:54:08 PST 2017
Thu Feb 23 20:54:13 PST 2017
Thu Feb 23 20:54:18 PST 2017
Thu Feb 23 20:54:23 PST 2017
hadoop is logged in
[root@localhost ~]# cat checkhadoop.sh
#!/bin/bash

who | grep "hadoop" &> /dev/null
ret=$?

while [ $ret -ne 0 ] ; do
 sleep 5
 echo "`date`"
 who | grep "hadoop" &> /dev/null
 ret=$?
done

echo "hadoop is logged in"
[root@localhost ~]#

写一个脚本:
1) 显示一个菜单给用户:
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
*) quit.
2) 当用户给定选项后显示相应的内容;
  
扩展:
 当用户选择完成,显示相应信息后,不退出;而让用户再一次选择,再次显示相应内容;除了用户使用quit;

[root@localhost ~]# bash showdisk.sh
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
*) quit.
your choice:d
disk usage:
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        18G  3.1G   14G  19% /
tmpfs           931M   72K  931M   1% /dev/shm
/dev/sda1       283M   33M  236M  13% /boot
your choice:m
memory usage:
Mem:          1861        452       1409          1         68        153
your choice:s
swap usage:
Swap:         2047          0       2047
your choice:quit
[root@localhost ~]# cat showdisk.sh
#!/bin/bash

cat << EOF
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
*) quit.
EOF

read -p "your choice:" choice

while [ $choice != 'quit' ]; do
case $choice in
d|D)
 echo "disk usage:"
 df -Ph
 ;;
m|M)
 echo "memory usage:"
 free -m | grep Mem
 ;;
s|S)
 echo "swap usage:"
 free -m | grep Swap
 ;;
*)
 echo "unkonw"
 exit 9
 ;;
esac
read -p "your choice:" choice

done
[root@localhost ~]#


循环控制:

continue
中断当前这一次循环,提前进入下一软循环

[root@localhost ~]

# bash +x sum4.sh
the sum is: 2550
[root@localhost ~]# cat sum4.sh
#!/bin/bash
let sum=0
let i=0

while [ $i -le 100 ]; do
 let i++
 if [ $[$i%2] -eq 1 ];then
  continue
 fi
 let sum+=$i
done

echo "the sum is: $sum"
[root@localhost ~]#

break
中断循环,而后执行循环后面的语句;

[root@localhost ~]# cat sum5.sh
#!/bin/bash

declare -i sum=0

for i in {1..10000};do
 let sum+=$i
 if [ $sum -gt 5000 ];then
  break
 fi
 let i++
done
echo "the sum is:$sum"
[root@localhost ~]# bash sum5.sh
the sum is:5050
[root@localhost ~]#

while特殊用法1--无限循环

[root@localhost ~]# bash while1.sh
file path:hello
hello not exist
file path:/etc/inittab
/etc/inittab exits
file path:quit
[root@localhost ~]# cat while1.sh
#!/bin/bash

while :;do
 read -p "file path:" filepath
 [ $filepath == "quit" ] && break
 if [ -e $filepath ];then
  echo "$filepath exits"
 else
  echo "$filepath not exist"
 fi
done
[root@localhost ~]#

while特殊用法2--按行读取文件

[root@localhost ~]# cat while2.sh
#!/bin/bash
file=/etc/passwd


while read line;do
 [ `echo $line | awk -F : '{print $7}'` ] && echo $line | awk -F : '{print $1}'
done < $file
[root@localhost ~]# bash while2.sh
root
bin
daemon
adm
lp
sync
shutdown
halt

以上是“bash脚本中如何使用while”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. python中if,for,while语句的使用
  2. bash脚本总结

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

bash while

上一篇:怎么实现Sharding Sphere读写分离

下一篇:Phoenix常见问题有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》