LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem with loop bash (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-loop-bash-938544/)

ofer4 04-06-2012 05:41 PM

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


catkin 04-06-2012 10:57 PM

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

ofer4 04-07-2012 03:14 AM

thanks you

can you explain me, what does the command "wc -l" ?

grail 04-07-2012 03:49 AM

Try your man pages.

arizonagroovejet 04-07-2012 04:15 AM

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.

ofer4 04-07-2012 04:04 PM

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?

arizonagroovejet 04-07-2012 04:16 PM

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 $

ofer4 04-07-2012 04:30 PM

my mistake, the first code do it

thanks alot i really appreciate your help

arizonagroovejet 04-07-2012 04:36 PM

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.

arizonagroovejet 04-07-2012 04:38 PM

Post 8 was completely rewritten whilst I was composing post 9.

ofer4 04-07-2012 04:52 PM

ok thanks,,

maybe do you have any ideas for this problem:

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


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