LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2009, 03:10 AM   #1
orbitalen
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
Problem with while loop


Hello everyone! I'm having a problem with this simple script...

ls -la /|awk '{print $8}'>list
while read line
do
if "$line"="boot"
then
echo "$line"
fi
done < list;

Can anyone help me? When I run it I get:

bash: =boot: command not found
bash: .=boot: command not found
bash: ..=boot: command not found
.
.
.
and all the way down untill the end ot the file...

Anyone!?
 
Old 09-03-2009, 03:12 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
if [ "$line" = "boot" ]
you have to use square brackets to perform a numeric or string comparison. Without them the expression is interpreted as a command, hence the command not found error. Indeed you can also test for commands using if/then without brackets: it will evaluate the exit status of the command (success or failure), but this is another story.

Last edited by colucix; 09-03-2009 at 03:15 AM.
 
Old 09-03-2009, 03:15 AM   #3
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
if [ "$line"="boot" ]
 
Old 09-03-2009, 03:15 AM   #4
orbitalen
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Ok, the error is gone now, but the output is the content of the file (the whole content), not just boot...
 
Old 09-03-2009, 03:18 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by orbitalen View Post
Ok, the error is gone now, but the output is the content of the file (the whole content), not just boot...
Given the lines of code you have posted, that's not possible. Maybe the entire content of the file is displayed from a command you didn't show us. What is the actual code of your script?
 
Old 09-03-2009, 03:21 AM   #6
orbitalen
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Well, that's it... I run the ls -la .... command in the command line to generate the file and then run:

# while read line; do if [ "$line"="boot" ]; then echo "$line"; fi done < list

And I get the content of the whole file...

The idea is to use this code (of course with lots of changes) in another script, but the mistake is the same here and there...
 
Old 09-03-2009, 03:22 AM   #7
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
I think that might actually need to be

Code:
if [ "$line" == "boot" ]
I dont get any output with that (coz there's no 'boot' in the line)
 
Old 09-03-2009, 03:24 AM   #8
orbitalen
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Admiral Beotch View Post
I think that might actually need to be

Code:
if [ "$line" == "boot" ]
I dont get any output with that (coz there's no 'boot' in the line)
OK... that worked! :P Thanks, guys!! Help appreciated!
 
Old 09-03-2009, 03:31 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Admiral Beotch View Post
I think that might actually need to be

Code:
if [ "$line" == "boot" ]
I dont get any output with that (coz there's no 'boot' in the line)
Not really true, indeed. In bash you can use either = and == for string comparison. The problem was that the OP didn't put spaces around the = sign. This resulted in a whole and unique string which is always evaluated as true.
 
Old 09-03-2009, 03:37 AM   #10
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Yeah I was kinda confused about that cause I just read the man page and it said = is the same as ==..

Persnickety. Thanks for that.
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
Problem with C loop ! linuxlover1 Programming 3 11-02-2006 03:32 PM
problem with do while loop. RHLinuxGUY Programming 12 02-06-2006 03:44 PM
loop problem andym Linux - Newbie 0 06-05-2003 05:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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