-1

I try to test do while loop in linux but showing error. script:

#!/bin/bash

a=10
do
{
echo "hello user"
} while(a <=20);

showing error: w.sh: line 4: syntax error near unexpected token do' w.sh: line 4:do'

please help me out to solve my query.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    You're mixing C syntax and shell syntax. Please look at some tutorials. – Mat Dec 02 '19 at 05:32
  • Hi, thanks for viewing my post, can you please reply with proper syntax of do while loop in linux/unix in bash shell. – pankaj srivastava Dec 02 '19 at 05:41
  • Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Dec 02 '19 at 05:58

1 Answers1

0

In bash, there isn't a simple do-while loop.

At first: the (normal) while loop

while <condition>; do
    <code>
done

As this answer states, you can emulate a do-while loop with a function or a break:

loopBody{
    <code>
}
loopBody
while <condition>; do
    loopBody
done

or

while true; do
    <code>
    condition || break
done
dan1st
  • 12,568
  • 8
  • 34
  • 67