KornShell (ksh) 是否有 Do...While 循环?
我的 KornShell (ksh) 脚本中有一个循环,我想至少执行一次,并且我想要一种优雅的方式来执行它,但是,虽然我发现了很多关于如何编写 while 循环的内容,但似乎没有为 do...while 循环做任何事情。
我正在做的是从配置文件中读取以逗号分隔的文件列表并处理它们。如果文件列表为空,那么我想处理目录中的所有文件。
有什么好的方法可以做到这一点?
编辑:这是我目前所拥有的。我获取文件名,然后将其从字符串中删除以进行下一次传递。如果文件列表为空,我将退出循环。但是,如果列表一开始就是空的,我希望它仍然运行一次。
while [[ -n "${FILES%%,*}" ]]; do
FILE="${FILES%%,*}"
FILES="${FILES#*,}"
done
I have a loop in my KornShell (ksh) script that I want to execute at least once, and I want an elegant way of doing it, however while I have found plenty of stuff on how to write a while loop, there does not seem to be anything for a do...while loop out there.
What I am doing is reading in a comma-delimited list of files from a configuration file and processing them. If the list of files is empty, then I want to process all files in the directory.
What is a good way to do this?
EDIT: Here is what I have currently. I grab the filename, then remove it from the string for the next pass. If the list of Files is empty, I quit the loop. BUT, if the list is empty to begin with, I want it to still run once.
while [[ -n "${FILES%%,*}" ]]; do
FILE="${FILES%%,*}"
FILES="${FILES#*,}"
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,
ksh
中的标准while
循环开箱即用地支持此功能:标准
while
循环之前有代码块> 和之后执行
。每个块可以包含多个命令。传统上我们只使用
第一个块的单个命令,其退出状态决定是否
循环终止或继续。
当我们使用多个命令时,只需
最后一个命令的状态很重要。
Yes, the standard
while
loop inksh
supports this out of the box:The standard
while
loop has code blocks before and afterdo
.Each block may contain multiple commands. Conventionally we use only
a single command for the first block, and its exit status determines whether
the loop terminates or is continued.
When we use multiple commands, only
the status of the last command matters.
你可以伪造它:
you could fake it:
ksh 中没有这样的构造。您可以通过在
while true; 末尾处的
break
(或continue
)来模拟这一点;做 ... ;完成循环。There is no such construct in ksh. You can emulate this by
break
(orcontinue
) at the end of awhile true; do ... ; done
loop.