脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - shell - shell脚本编程之循环语句学习笔记

shell脚本编程之循环语句学习笔记

2023-02-20 14:17shell教程网 shell

这篇文章主要介绍了shell脚本编程之循环语句学习笔记,本文内容较简单,可以作为shell循环语句的备忘录,忘记怎么写时来看看吧~需要的朋友可以参考下

本篇博客很简单,看一下shell编程使用到的循环语句,包括for循环,while循环,until循环,for后边跟一个变量,然后是一个集合,将集合中的东西赋给这个变量,每次循环执行,这跟java中的foreach很像,while循环和if使用同样的条件判断,满足条件执行语句,until和while相反,不满足条件执行语句,是不是很简单啊,下面看一下代码吧。

 

复制代码 代码如下:


#!/bin/sh
#for循环最基本的用法
for var in "hello" "xiao ta" "welcome to www.tuohang.net"
do
echo -n "$var "
done
echo
#通配符扩展
for var in $(ls *.sh)
do
echo "$var"
done

 

#while循环,后边和if一样跟的都是条件
echo "please input secret"
read secret
while [ "$secret" != "xiao ta" ]
do
echo "try again"
read secret
done

#until循环和while相反,条件为假才执行
echo "please input text"
read text
until [ "$text" = "xiao ta" ]
do
echo "try again"
read text
done

exit 0

 

延伸 · 阅读

精彩推荐