LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 08-23-2004, 01:04 AM   #1
junjem0702
LQ Newbie
 
Registered: Jul 2004
Posts: 9

Rep: Reputation: 0
If Statement


Hi guys,

Im doing program in linux shell that search a text within a file and display the result of a text including the filename of a file where it exists. This is a sample program I made..

#!/bin/bash

cd /home/vpopmail/domains/domain.com
list=`ls .qmail-*`

for filename in $list
do
if [`cat $filename | grep $1`]
then
echo $filename
fi
done
exit 0



if [`cat $filename | grep $1`], im not sure with this line. TY
 
Old 08-23-2004, 01:34 AM   #2
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
Re: If Statement

Quote:
Originally posted by junjem0702
Hi guys,

Im doing program in linux shell that search a text within a file and display the result of a text including the filename of a file where it exists. This is a sample program I made..

#!/bin/bash

cd /home/vpopmail/domains/domain.com
list=`ls .qmail-*`

for filename in $list
do
if [`cat $filename | grep $1`]
then
echo $filename
fi
done
exit 0



if [`cat $filename | grep $1`], im not sure with this line. TY
Try

Code:
if [ "`cat $filename | grep $1`" != "" ];
then
echo "file contains $1";
fi
notice the `;' at the end of line also..
 
Old 08-23-2004, 02:55 AM   #3
sterrenkijker
Member
 
Registered: Mar 2004
Location: the Netherlands
Distribution: Debian Sarge
Posts: 302

Rep: Reputation: 30
Code:
if grep -q $1 $filename
is a more elegant and efficient way of achieving what you want. grep -q returns true if the string is found in the file.
 
Old 08-23-2004, 08:31 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Here's a short solution:

Code:
for file in `find . -name '*.log' -print 2>/dev/null`
do
    RSLT=`grep $1 $file`
    if [[ $? -eq 0 ]]
    then
        echo $file
    fi
done

Last edited by chrism01; 08-23-2004 at 08:33 AM.
 
Old 08-23-2004, 09:49 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
here's an even shorter one:

Code:
grep -l     /pattern/     .qmail-*
 
  


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
net use statement mnauta Linux - Networking 1 08-19-2005 01:21 PM
the FOR statement (C) sk84blood813 Programming 3 12-09-2004 08:25 AM
For Statement bendeco13 Linux - General 6 10-31-2004 05:31 PM
switch statement linuxanswer Programming 2 11-29-2003 11:25 PM
mysql statement... johnyy Linux - Software 1 11-02-2003 03:30 AM


All times are GMT -5. The time now is 09:03 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration