While循环在Bash脚本? [英] While loop in Bash script?
问题描述
我不习惯写的bash脚本,而谷歌并没有搞清楚帮助什么是错的这个脚本:
#!/斌/庆典
而读MYLINE
做
DONE回声你好
而读线
做
DONE退出0
我得到的输出是:
./ basic.agi:4号线:语法附近意外的标记错误完成
./basic.agi:4号线:'做'
和我的庆典
的版本是:
GNU bash中,3.2.25版(1) - 释放(i686的-红帽Linux的GNU的)
感谢您。
编辑:。该脚本确定,当while循环不为空
而我在它...我希望当用户输入什么,即退出循环。只需按下回车键,但猛砸不断循环。我怎样才能退出循环?
同时读取MYLINE
做
回声$ {} MYLINE
DONE回声你好
而读线
做
真正
DONE退出0
您不能有一个空循环。如果你想要一个占位符,只要使用真正
。
#!/斌/庆典
而读MYLINE
做
真正
DONE
,或者更可能的是,做一些与输入有用的:
#!/斌/庆典
而读MYLINE
做
回声您输入[$行]
DONE
至于你的第二个问题,关于如何退出循环时,用户只需presses <大骨节病> ENTER 骨节病>有没有别的,你可以做一些事情:
#!/斌/庆典
在线阅读
而[[$行=]!]做
回声您输入[$行]
在线阅读
DONE
I'm not used to writing Bash scripts, and Google didn't help in figuring out what is wrong with this script:
#!/bin/bash
while read myline
do
done
echo "Hello"
while read line
do
done
exit 0
The output I get is:
./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'
and my bash
version is:
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Thank you.
Edit: The script works OK when the while loop isn't empty.
While I'm at it... I expected to exit the loop when the user typed nothing, ie. simply hit the Enter key, but Bash keeps looping. How can I exit the loop?
while read myline
do
echo ${myline}
done
echo "Hello"
while read line
do
true
done
exit 0
You can't have an empty loop. If you want a placeholder, just use true
.
#!/bin/bash
while read myline
do
true
done
or, more likely, do something useful with the input:
#!/bin/bash
while read myline
do
echo "You entered [$line]"
done
As for your second question, on how to exit the loop when the user just presses ENTER with nothing else, you can do something:
#!/bin/bash
read line
while [[ "$line" != "" ]] ; do
echo "You entered [$line]"
read line
done
这篇关于While循环在Bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!