LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-01-2012, 02:48 PM   #1
kiki2009
LQ Newbie
 
Registered: Oct 2009
Posts: 4

Rep: Reputation: 0
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'
 
Old 10-01-2012, 02:58 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,615

Rep: Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577
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
 
1 members found this post helpful.
Old 10-01-2012, 03:16 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Also, as you are using bash I would recommend using [[ over [ (see here for more info) and (( over [[ when performing
arithmetic expressions (see here)
 
1 members found this post helpful.
Old 10-02-2012, 09:14 AM   #4
kiki2009
LQ Newbie
 
Registered: Oct 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thank alots for that information. Its helps me to learn since i'm new to linux.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell scripting - for...in loop and variables question kcleveland Programming 12 11-20-2011 09:17 AM
[SOLVED] Shell scripting loop error kathirvel Programming 5 07-28-2011 03:06 AM
Until loop in bash shell scripting help computergeek7 Programming 6 03-02-2010 12:34 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 03:30 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration