LinuxQuestions.org
Visit Jeremy's Blog.
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 04-18-2004, 05:29 PM   #1
darin3200
Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Rep: Reputation: 45
Script to check for contents in a file


I was wondering how to make a script that would periodically check for certain contents in a file ( a line of text) and then run another script depending on the contents matched. Any ideas on how to go about doing this?
Thanks
 
Old 04-18-2004, 06:56 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Your script is a good candidate for being a cron job.
see man cron crontab
 
Old 04-19-2004, 07:22 PM   #3
naflan
Member
 
Registered: May 2003
Location: NC, USA
Distribution: Slackware 13.0
Posts: 91

Rep: Reputation: 15
to check for a line of text use grep.
 
Old 04-19-2004, 07:33 PM   #4
darin3200
Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Original Poster
Rep: Reputation: 45
Ok, thanks for all the help so far
 
Old 04-24-2004, 10:31 PM   #5
darin3200
Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Original Poster
Rep: Reputation: 45
ok, i see how grep works but how do I get it so that when it finds the text does something like run a program
 
Old 04-24-2004, 11:56 PM   #6
Technoslave
Member
 
Registered: Dec 2003
Location: Northern VA
Posts: 493

Rep: Reputation: 30
(* jhill@gimp pts/5 12:55 AM 43 *) - ~/prog
> if [ `grep -c jason blah` != 0 ]; then ./echoit.sh; fi
Found

> more echoit.sh
#!/bin/sh

echo Found
 
Old 04-25-2004, 04:22 PM   #7
darin3200
Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Original Poster
Rep: Reputation: 45
Code:
#!/bin/bash
if [ 'grep -c darin testfile' != 0 ]; then echo right
else echo wrong
fi
The testfile contains the word 'test'

Whenever I run the run the script I will get the output saying 'right' regardless of the contents of the file. How do I fix this?

Last edited by darin3200; 04-25-2004 at 04:23 PM.
 
Old 04-25-2004, 05:26 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
#!/bin/bash
if grep -q darin testfile
then
echo "darin is there !"
else
echo "where is darin ?"
fi
 
  


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 script to compare dir contents Boffy Programming 2 08-02-2005 06:08 AM
check file permissions in a script cambie Linux - General 9 09-22-2004 12:13 AM
How can I check wether a file is empty in Linux by using shell script programming ??? Bassam Linux - General 2 02-26-2004 01:01 AM
Delete contents of the file, but not file? mikeshn Linux - General 9 05-28-2003 06:47 AM
Need script to check file size. _TK_ Linux - General 1 08-06-2001 08:30 AM


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