10

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
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Gulbahar
  • 5,343
  • 20
  • 70
  • 93

6 Answers6

17

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
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

When you are using the read command in a while loop it need input:

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

or using several lines:

echo "Hello" | while read line
    do
    echo $line
done
Anders Zommarin
  • 7,094
  • 2
  • 25
  • 24
1

This is a way to emulate a do while loop in Bash, It always executes once and does the test at the end.

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
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Dennis, did you mean to init `line` with something other than `''`? It appears to me that the loop will never start. My understanding is that until is simply the negative condition version of while in that it doesn't guarantee a minimum execution of one iteration. I may be wrong but the bash manpage seems to indicate that's the case as well. – paxdiablo Jul 22 '13 at 22:53
  • @paxdiablo: You are correct. Other languages have a proper `do` loop with a test at the end, but Bash does not. My answer [here](http://stackoverflow.com/a/16491478/26428) shows how to emulate a `do` `while` loop in Bash. I've updated this answer to match that one. – Dennis Williamson Jul 23 '13 at 00:14
0

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.

Sedrik
  • 2,161
  • 17
  • 17
0

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

Benbentwo
  • 391
  • 1
  • 2
  • 10
-1

If you type help while in bash you'll get an explanation of the command.

onemasse
  • 6,514
  • 8
  • 32
  • 37