Linux Bash脚本中的while循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5104426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
While loop in Bash script?
提问by Gulbahar
I'm not used to writing Bash scripts, and Google didn't help in figuring out what is wrong with this script:
我不习惯编写 Bash 脚本,谷歌也没有帮助找出这个脚本有什么问题:
#!/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:
我的bash
版本是:
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 循环不为空时,脚本工作正常。
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?
当我在做的时候......当用户什么都不输入时,我希望退出循环,即。只需按 Enter 键,Bash 就会一直循环。我怎样才能退出循环?
while read myline
do
echo ${myline}
done
echo "Hello"
while read line
do
true
done
exit 0
采纳答案by paxdiablo
You can't have an empty loop. If you want a placeholder, just use true
.
你不能有一个空循环。如果你想要一个占位符,只需使用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 ENTERwith nothing else, you can do something:
至于你的第二个问题,关于如何在用户不按ENTER任何其他东西时退出循环,你可以做一些事情:
#!/bin/bash
read line
while [[ "$line" != "" ]] ; do
echo "You entered [$line]"
read line
done
回答by onemasse
If you type help while
in bash you'll get an explanation of the command.
如果你输入help while
bash 你会得到命令的解释。
回答by Anders Zommarin
When you are using the read
command in a while loop it need input:
当您read
在 while 循环中使用该命令时,它需要输入:
echo "Hello" | while read line ; do echo $line ; done
or using several lines:
或使用几行:
echo "Hello" | while read line
do
echo $line
done
回答by Sedrik
What are you trying to do? From the look of it it seems that you are trying to read into a variable?
你想做什么?从它的外观来看,您似乎正在尝试读入一个变量?
This is done by simply stating read the value can then be found inside of $
这是通过简单地说明读取值然后可以在 $ 中找到来完成的
ex:
前任:
read myvar
echo $myvar
As other have stated the trouble with the loop is that it is empty which is not allowed.
正如其他人所说,循环的问题在于它是空的,这是不允许的。
回答by Paused until further notice.
This is a way to emulate a do
while
loop in Bash, It always executes once and does the test at the end.
这是一种do
while
在 Bash 中模拟循环的方法,它总是执行一次并在最后进行测试。
while
read -r line
[[ $line ]]
do
:
done
When an empty line is entered, the loop exits. The variable will be empty at that point, but you could set another variable to its value to preserve it, if needed.
当输入空行时,循环退出。此时变量将为空,但如果需要,您可以将另一个变量设置为其值以保留它。
while
save=$line
read -r line
[[ $line ]]
do
:
done
回答by Benbentwo
If you are looking to create a menu with choices as a infinite loop (with the choice to break out)
如果您希望创建一个带有无限循环选项的菜单(可以选择突破)
PS3='Please enter your choice: '
options=("hello" "date" "quit")
select opt in "${options[@]}"
do
case $opt in
"hello") echo "world";;
"date") echo $(date);;
"quit")
break;;
*) echo "invalid option";;
esac
done