简体   繁体   English

在Bash脚本中循环?

[英]While loop in Bash script?

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. 只需按Enter键,但Bash保持循环。 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 . 如果你想要一个占位符,只需使用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: 至于你的第二个问题,关于如何在用户只需按ENTER键时退出循环,你可以做一些事情:

#!/bin/bash
read line
while [[ "$line" != "" ]] ; do
    echo "You entered [$line]"
    read line
done

When you are using the read command in a while loop it need input: 在while循环中使用read命令时,需要输入:

echo "Hello" | while read line ; do echo $line ; done

or using several lines: 或使用几行:

echo "Hello" | while read line
    do
    echo $line
done

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. 正如其他人所说,循环的问题在于它是空的,这是不允许的。

This is a way to emulate a do while loop in Bash, It always executes once and does the test at the end. 这是一种在Bash中模拟do while循环的方法,它总是执行一次并在最后进行测试。

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

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

PS3 is described here PS3在这里描述

如果您在bash中键入help while您将获得该命令的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM