LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-06-2012, 06:41 PM   #1
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Rep: Reputation: Disabled
problem with loop bash


hi

i wrote this code:

the code print how much file in the sub folder logs,which end with inout.log. ,
contain "hello" word at least one time

however the code does not work for me(it does not print anything)...any ideas please?

thanks alot
Code:
#!/bin/bash

count=0

for file in logs/*inout.log.; do


	while read line; do
	
	cat $line | grep "hello"
	
	if [[ $? -eq 0 ]];then (( count++ )); 
	
	fi
	
	done
	
done

Last edited by ofer4; 04-06-2012 at 06:52 PM.
 
Old 04-06-2012, 11:57 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
cat $line | grep "hello" sends the contents of a file called $line (which almost certainly does not exist) to grep.

Incidentally, you probably don't want the "." after log.

This would do what you want in one line: grep hello logs/*inout.log | wc -l
 
Old 04-07-2012, 04:14 AM   #3
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
thanks you

can you explain me, what does the command "wc -l" ?
 
Old 04-07-2012, 04:49 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Try your man pages.
 
Old 04-07-2012, 05:15 AM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
For reference, you would have been closer to your goal using echo instead of cat
Code:
echo $line | grep "hello"
but your code still wouldn't have worked because you code doesn't assign any value to $line and you would have been writing a lot more code than you need.

I don't think catkin's example does what you want either. catkin's example will give you the total number of lines on which 'hello' appears but when I read what you've written I think that you want the number of files in which hello appears. The two things are only the same if no file contains more than one line that contains 'hello'. What you've written does leave some room for interpretation.

Assuming I'm correct (which I might not be), then I think you want is
Code:
$ grep -l hello logs/*inout.log | wc -l
Look at grep's man page to see what the -l option does. You can also try running both
Code:
$ grep hello logs/*inout.log
and
Code:
$ grep -l hello logs/*inout.log
and compare the output.
 
Old 04-07-2012, 05:04 PM   #6
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
hi

thanks for help

i tried to run all the three codes that you wrote...

(i have a file which named "1.inout.log" in "logs" directory and i wrote then hello man...

so the output of file must be 1...which does not seems to be like)

moreover,there is an error, please read the output:

Code:
3.3: line 3: $: command not found
0
3.3: line 5: $: command not found
3.3: line 7: $: command not found
can you help me please?

Last edited by ofer4; 04-07-2012 at 05:08 PM.
 
Old 04-07-2012, 05:16 PM   #7
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
You've completely changed the content of your post since I got the email notification. If you'd edit posts it's a good idea to say you've done it and why.

Most of what you currently have in the post as I'm writing this doesn't make any sense to me but I can tell you that you should not include the $ in my examples. The $ represents the command prompt. Your command prompt ,any not actually be a $ but it's a long standing convention used when giving example commands to use $ to represent the command prompt.

tl;dr don't type the $

Last edited by arizonagroovejet; 04-07-2012 at 05:16 PM. Reason: fixed missing space
 
Old 04-07-2012, 05:30 PM   #8
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
my mistake, the first code do it

thanks alot i really appreciate your help

Last edited by ofer4; 04-07-2012 at 05:33 PM.
 
Old 04-07-2012, 05:36 PM   #9
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Post the output you get from each of the three commands. At least one of them should give output which is different to the other two. I believe one of the commands should give the output you want.
 
Old 04-07-2012, 05:38 PM   #10
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Post 8 was completely rewritten whilst I was composing post 9.
 
Old 04-07-2012, 05:52 PM   #11
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
ok thanks,,

maybe do you have any ideas for this problem:

http://www.linuxquestions.org/questi...730/page2.html
 
  


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
[Bash] Problem with arguments in loop en1du Programming 6 10-26-2011 07:53 AM
[SOLVED] Formating problem in bash while loop k_balaa Linux - Newbie 9 09-01-2011 10:42 AM
Bash infinite loop problem? stp001 Linux - Newbie 6 03-15-2010 07:56 AM
bash while loop problem MadMusician_uk Programming 3 04-20-2006 12:52 PM
bash for loop problem deadlock Programming 5 09-04-2003 05:32 AM


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