shell循环控制语句:
for、while、until、select
这篇文章我们来学习下until的用法
Note:until语句和while语句正好相反,while语句判断条件为真时,进行循环操作
而until则是判断条件为假时,进行循环操作
一、until语法结构
until 条件测试
do
命令区域
done
二、until常见用法
借用case文章中的示例:我们修改下来使用
[root@lovelace case]# cat 2showmenu.sh #!/bin/bash #Version:0.1.1 #Auther:lovelace #pragram:show menu and wait user choice # #difine an function to display the menu showmenu(){ echo -n '+----------' echo -n -e "\033[1;32mshowmenu\033[0m" echo '-----------+' echo -e "| \033[31md|D) show disk information\033[0m |" echo -e "| \033[31mm|M) show memory usage\033[0m |" echo -e "| \033[31ms|S) show swap usage\033[0m |" echo -e "| \033[31mq|Q) quitting\033[0m |" echo -n '+-------------' echo -n -e "\033[5,33mEND\033[0m" echo '--------------+' } #call showmenu function echo showmenu echo #read the argument for user input read -p "Your choice is:" choice #use until statement to loop until [ "$choice" == "q" -o "$choice" == "Q" ];do case $choice in d|D) echo "Disk information:" df -h ;; m|M) echo "Memory information:" free -m | grep "Mem" ;; s|S) echo "Swap information:" free -m | grep "Swap" ;; q|Q) echo "quitting...." exit 8 ;; *) echo "Unknow argument." ;; esac #call showmenu function again echo showmenu echo #read user input again read -p "Please select again:" choice done
总结:从这几篇文章中可以看出来,其实每个语句都可以实现同样的功能,而有区别的是语句中的判断条件的不同,所以要想熟练的写出能在实际生产环境中运行的脚本,基础知识一定要打牢固了,从一系列文章中我们不能看出几点:
1、基本命令的使用
2、变量的灵活运用
3、shell各种语句的灵活掌握
4、各种条件判断的掌握
5、正则表达式的掌握(这个在shell编程中至关重要)
6、长期实战经验的积累
总的来说:学习shell,三点:多练、多看、多总结
练:尽可能和实际生产环境靠拢,因为我们学习shell不是玩的,而是要实现某种我们需要的功能的
看:看别人写的好的脚本,看系统上的脚本,看被大家津津乐道的脚本
总结:在自我练习的过程中,碰到文件要及时记录,并查找相关文档或咨询他人来破解谜团,看的时候觉得经典的语句或命令要做记录,以归纳总结为自己的知识。。。。
PS:可能有人会说,你这是什么玩意额,纯属自娱自乐,写的程序和实际生产环境一点都不搭嘎,学了有毛用额,对此,我只能笑而不语......
转载于:https://blog.51cto.com/lovelace/1212091