LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Cannot compile while loop in shell scripting (https://www.linuxquestions.org/questions/linux-newbie-8/cannot-compile-while-loop-in-shell-scripting-4175429927/)

kiki2009 10-01-2012 01:48 PM

Cannot compile while loop in shell scripting
 
Dear Expert,
I try to compile this shell script it give me error

Code:

#!/bin/bash
#set n to 1
n=1
#continue until $n equals 5
while[$n -le 5]
do
  echo "Welcome $n times."
  n=$(( n+1 ))  #increments $n
done

Here is the error shown on terminal

Code:


./while.sh: line 5: while[1 -le 5]: command not found
./while.sh: line 6: syntax error near unexpected token `do'
./while.sh: line 6: `do'


rknichols 10-01-2012 01:58 PM

The '[' character is a command, not a builtin part of the "while" syntax. Both the '[' and ']' characters need to be delimited by white space:
Code:

while [ $n -le 5 ]
do
.
.
.
done


grail 10-01-2012 02:16 PM

Also, as you are using bash I would recommend using [[ over [ (see here for more info) and (( over [[ when performing
arithmetic expressions (see here)

kiki2009 10-02-2012 08:14 AM

Thank alots for that information. Its helps me to learn since i'm new to linux.


All times are GMT -5. The time now is 10:41 AM.