LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-29-2013, 01:11 PM   #1
hudac
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Rep: Reputation: Disabled
Creating an action based event in bash


Hey,
I couldn't find an answer to my question, and I'm sure it is possible:

How can I create an action based on some event in bash:

Lets say I have some file `text.txt`,
I want to do something, like `grep` on this file
If and only if a line was added to this file,
Or the file size got bigger, or md5sum...

Thanks!
 
Old 05-29-2013, 02:31 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
W/o knowing any requirements (when, how much, how long or whatever else) I'd say that sounds like a job for Inotify.
 
Old 05-29-2013, 03:24 PM   #3
hudac
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanks,

Actually, It suppose to be an action that, lets say, start "gnuplot", every time a line is added to the file `text.txt`
There is no more options given...

Does the question is clearer now ?
Does it gives more information that may offer another solution?
Or should I just use `inotify` ?


Thanks!
 
Old 05-29-2013, 04:36 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Inotify would work for that, yes. However inotify reacts immediately so unless you script carefully and check for already an running gnuplot process you might find you end up running a gazillion processes if the file gets added to continuously. This example effectively only echoes the file name:
Code:
#!/bin/sh --
LANG=C; LC_ALL=C; export LANG LC_ALL
/usr/bin/pgrep gnuplot >/dev/null 2>&1 && exit 0
/usr/bin/inotifywait -m -q -e CLOSE_WRITE /path/to/text.txt --format '%w%f'\
|while read ITEM; do echo "${ITEM}"; done; exit 0
 
Old 05-29-2013, 06:24 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
You could write a small script to track the new lines being added, based on the cmd 'tail -f test.txt' in bash.
Alternatively, Perl has a module designed for this; see definition & example code http://search.cpan.org/~mgrabnar/Fil...0.99.3/Tail.pm
 
Old 05-30-2013, 12:46 AM   #6
hudac
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Great, Thanks!
 
  


Reply

Tags
bash, bash scripting, linux, script



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
Need help creating bash script that creates directories based on what you answer Profound_Darkness Linux - General 10 03-26-2013 10:34 AM
creating a text based menu structure in bash 1yuppie Programming 4 05-17-2009 10:39 AM
Event based task scheduler? beeblequix Linux - Software 1 01-30-2008 11:49 AM
Action/Event/Change handlers in Java Swing plk Programming 2 06-21-2007 04:01 AM
Perform action based on email txt plisken Linux - General 1 03-06-2007 09:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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