LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 01: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, 01:58 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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, 02:16 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 08: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



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 08:17 AM
[SOLVED] Shell scripting loop error kathirvel Programming 5 07-28-2011 02:06 AM
Until loop in bash shell scripting help computergeek7 Programming 6 03-01-2010 11:34 PM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM

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

All times are GMT -5. The time now is 11:10 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
Open Source Consulting | Domain Registration