Bash 脚本中的 while 循环?

2024-02-11

我不习惯编写 Bash 脚本,Google 也没有帮助找出这个脚本有什么问题:

#!/bin/bash
while read myline
do
done

echo "Hello"
while read line
do
done

exit 0

我得到的输出是:

./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'

and my bash版本是:

GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)

谢谢。


编辑:当 while 循环不为空时,脚本可以正常工作。

当我这样做时......我希望当用户什么都不输入时退出循环,即。只需按 Enter 键,但 Bash 会一直循环。我怎样才能退出循环?

while read myline
do
        echo ${myline}
done

echo "Hello"
while read line
do
        true
done

exit 0

你不能有一个空循环。如果您想要占位符,只需使用true.

#!/bin/bash
while read myline
do
    true
done

或者,更有可能的是,对输入做一些有用的事情:

#!/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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Bash 脚本中的 while 循环? 的相关文章

随机推荐