LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-22-2011, 02:48 PM   #1
manish23
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
problem in shell script


i am having problem in shell script

i m following below steps:
[root@locathost bin]# gedit hello.sh

>> code in hello.sh
n=0
while [$n -le 6 ];
do
echo $n
$n = `expr $n=$n+1`
done

then save exit

] # chmod +x hello.sh
] # ./hello.sh

then its comes
line 0: command not found
while not found
 
Old 07-22-2011, 02:54 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
Try the following:

Code:
n=0
while [ $n -le 6 ];
do
  echo $n
  n=$[$n+1]
done
Note the you need a space between [ and $n.
Additionally, it's not advisable to practise anything as root!!!!

Last edited by sycamorex; 07-22-2011 at 03:20 PM.
 
Old 07-22-2011, 02:59 PM   #3
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
You've got no shebang line to identify what shell it should run in. I can't speak for other shells, but in bash, 'while' is a shell built-in function.

So if this is to be run in bash, your first line needs to be:
Code:
#!/bin/bash
Also, you need spaces around each side of your braces in the while statement:
Code:
while [ $n -le 6 ];
 
Old 07-22-2011, 02:59 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Welcome to LinuxQuestions! This is a working version of your code:
Code:
n=0
while [ $n -le 6 ]
do
  echo $n
  n=`expr $n + 1`
done
Basically, you have to pay attention to spaces and to the syntax for variable assignment (n=something) and variable reference ($n, that is the value of the variable n).

Edit: too late!
 
Old 07-22-2011, 04:52 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by manish23 View Post
[root@locathost bin]# gedit hello.sh
Don't run X as root!!!

Quote:
Originally Posted by manish23 View Post
Code:
while [$n -le 6 ];
"[" is a command, not some kind of expression syntax. This means that it must have spaces between the arguments. Also, bash does have a true expression syntax:

Code:
while (( n <= 6 ))
And finally, when "do" is on the next line, the semicolon is not needed at the end of the "while" line.

Quote:
Originally Posted by manish23 View Post
Code:
$n = `expr $n=$n+1`
When assigning to variables, you don't use the "$" and there may not be spaces around the "=" sign. Also, use $(command) instead of backticks, since it's easier to nest and can't be confused with commas. Finally, why not just use bash's builtin arithmetic functionality:

Code:
(( n = n + 1 ))
Or you can make it even shorter:

Code:
(( n++ ))
Note that unlike "[]" which is a command, "(())" really is an expression syntax and spaces are not necessary between everything. I still like to use spaces because I think they make the code easier to read.
 
  


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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
shell script problem. chooi Programming 7 03-26-2005 06:16 PM
shell script problem steltner Linux - General 19 05-18-2003 03:42 PM

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

All times are GMT -5. The time now is 12:59 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