重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

bash脚本中如何使用while

这篇文章主要为大家展示了“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”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


文章标题:bash脚本中如何使用while
文章起源:http://cqcxhl.cn/article/jgpgpc.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP