Visit Our UNIX and Linux User Community


Bash script to notify when ever any files are changed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to notify when ever any files are changed
# 1  
Old 08-07-2013
Linux Bash script to notify when ever any files are changed

Hi the following script let sthe user know whenevr any file is changed inserted or deleted in file system.
but i am getting following error while running bash script

Code:
## LINUX SYSTEM FILE ARCHIVE NOTIFY ##
if [ ! $# -eq 2 ] ; then echo "Usage '$0 folder waitseconds' " ; exit 1; fi
if [ ! -d $1 ] ; then echo "Folder name is false!!" ; exit 1 ;fi
cd /usr/local/bin ; clear ; tmp='tmppx' ; tmpp="${tmp}x"
f="find '$1' -not \( -name $tmp -o -name $tmpp \) -exec stat -c '%s %n' {} \;"
eval $f >$tmp ; echo -e "Please wait.. Preparing File Lists!!" ; sleep $2 || exit 1 && echo "Lists OK!!"
eval $f >$tmpp ; lcltmp=lcltmp;lcltmpp="${lcltmp}x";lcltmppp="${lcltmpp}x"
cretdif() {
diff $tmp $tmpp|sed -n '/^[><]/p'|sort -k3,3>$lcltmp
}
chksz() {
dtecs=$(awk '{print $3}' $lcltmp|uniq -d)
}
rmvdupf() {
for dup in $dtecs ; do sed 's|.*'$dup'$||;/^$/d' $lcltmp>$lcltmpp && mv $lcltmpp $lcltmp -f;done
}
fnddupf() {
>$lcltmppp
for dup in $(echo "$dtecs"|sed 's|/|\\&|g') ; do
sed -n '/.*'$dup'$/p' $lcltmp>>$lcltmppp;done
}
chkfls() {
newrem=$(sed -n '/^['$1']/p' $lcltmp)
}
fix() {
sed -n 'N;s/[^ ]* \([^ ]*\) \([^ ]*\)\n\([^ ]*\) \([^ ]*\) \2$/\2 \1 \4 \3/p' $lcltmppp|
awk  'BEGIN {printf "%20s" "%30s\n" "%15s" "%10s\n" ,"File  Name(s)","Difference (Bytes)","-----------------------------","    ------------------------"};
{if($4==">")printf "%20s" "%10c" "%d" "%2s", $1 ,"+",$3-$2, " bytes";
else printf "%20s" "%10d" "%2s\n", $1, $2-$3, " bytes"};
END { printf "\n%31s\n%19s\n\n" ,"[End of summary list]","#########################################################" }'
}
cretdif;if [ ! -s $lcltmp ] ; then echo -e "No any changes!!..";exit 0;fi
chksz;if  [ ! "$dtecs" = "" ] ; then printf "%s\n%34s\n%30s\n"  "#########################################################" "[Detected  change of size]" "-----------------";fnddupf;fix;fi;rmvdupf
chkfls '>';if [ ! "$newrem" = "" ] ; then 
printf  "\n%s\n%34s%+61s\n%+30s%70s\n"  "#########################################################" "[Detected  new created files]" "[Type]" "---------------------" "----------------"
echo  "$newrem"|awk '{if (!system("test -d " $3) )printf "%-60s"  "%+36s\n",$0,"directory"; else printf "%-60s" "%+34s\n" ,$0,"file"};
END { printf "\n%31s\n%19s\n\n" ," [End of summary list]","#########################################################" }';fi
chkfls '<';if [ ! "$newrem" = "" ] ; then 
printf  "\n%s\n%34s%+61s\n%+30s%70s\n"  "#########################################################" "[Detected  removed files]" "[Type]" "-----------------" "----------------"
echo  "$newrem"|awk '{if (!system("test -d " $3) )printf "%-60s"  "%+36s\n",$0,"directory"; else printf "%-60s" "%+34s\n" ,$0,"file"};
END { printf "\n%31s\n%19s\n\n" ,"[End of summary list]","#########################################################"}';fi


ERROR:

<html>
<body>

'/chkmodifsearch.sh: line 8: syntax error near unexpected token `{
'/chkmodifsearch.sh: line 8: `cretdif() {

</body>
</html>


PHP CODE;
i am trying to call the bash script from php by using the following code.


<html>
<body>

<?php

$i=0;

$output = shell_exec ("./chkmodifsearch.sh");
while ($i < count($output))
{
echo "The Search Data is " . $output[$i] . "<br>";
$i++;
}

?>

Kindly assist me why the unexpected token `{ error is being generated. If sed awk '' and `` thing needs to be done please do it in the bash script

</body>
</html>

Last edited by jim mcnamara; 08-07-2013 at 06:14 PM..
# 2  
Old 08-07-2013
Aside from the fact that this script is a great way to convince people that one-liners are hard to read and maintain, I would guess you did not write it from scratch. Since you do not appear to understand it.

Generally, we are not a code correction service. There are syntax errors in there, but the formatting and structure obfuscates that code to the extent that reading it is difficult.

Maybe some one else can see the problems right away.

What I would do is comment out most of it. Then run it from a command line bit by bit uncommenting and correcting code until it works.

BTW: if you are on Linux consider inotify - it does most of what this script purports to do.

Previous Thread | Next Thread
Test Your Knowledge in Computers #249
Difficulty: Easy
Senator Albert Gore, Jr. authored the High Performance Computing and Communication Act of 1991, creating what Gore referred to as the information superhighway.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given. Please help me to understanding why node_port values are... (5 Replies)
Discussion started by: tmalik79
5 Replies

2. Shell Programming and Scripting

Notify as soon as an error is encountered in a script

Hi, The script below works okay and emails me the log in the end once the script completes but what I'm trying to do is to also notify me via an email as soon as the script encounters any error whatsoever. cat test.list hdisk0 00a6351a2c832da1 rootvg ... (7 Replies)
Discussion started by: mbak
7 Replies

3. Shell Programming and Scripting

Shell script to notify of service down

Hi All I am trying to write a shell script that will notify via email if a particular service is down. What I have so far is a script in cron like his: #!/bin/sh cd /usr/jdk/instances/jdk1.6.0/bin/sparcv9 jps -m And the output of the above is 81529 Jps 52988 TaskControllerService... (5 Replies)
Discussion started by: fretagi
5 Replies

4. Shell Programming and Scripting

Script to echo "File permissions or ownership changed from required " when accidentally changed.

Hi All, I have to work in the late nights some times for server maintenance and in a hurry to complete I am accidentally changing ownership or permission of directories :( which have similar names ( /var in root and var of some other directory ).:confused: Can some one suggest me with the... (1 Reply)
Discussion started by: shiek.kaleem
1 Replies

5. Shell Programming and Scripting

[linux] tail2notify - script interface between tail -f + grep and notify-send

This isn't exactly a question. Just thought I'd share something I just wrote and found useful. For those of you on modern linux boxen: you may be aware that there's a lovely little tool called notify-send that you can use to send notifications to the desktop. Any experienced shell-scripter could... (0 Replies)
Discussion started by: ryran
0 Replies

6. Shell Programming and Scripting

notify-send does not notify real time

Hi, I am having a little trouble getting notify-send to work the way I would like it to. I am using ubuntu - karmic koala 2.6.31-19-generic #56-Ubuntu SMP So here's the problem run the following commands one after the other. notify-send -i info -t 100000 -- "Hi" "world" & notify-send -i... (3 Replies)
Discussion started by: linuxpenguin
3 Replies

7. Shell Programming and Scripting

How to write a shell script to notify when certain texts appear in a file?

I have a server and occasionally the file mysqld.log would show something like /usr/libexec/mysqld: Disk is full writing './example_com_-_wordpress/wp_statpress.MYD' (Errcode: 122). Waiting for someone to free space... Retry in 60 secs How do I write a simple shell script to check mysqld.log... (1 Reply)
Discussion started by: acider
1 Replies

8. AIX

script to ftp recent changed files

I am trying to write a script to ftp all files/directories changed in a 24hour period to another AIX server. I have wrote a script to generate a list of all files changed within a 24 hour period but dont know how to then ftp these to another server. How do incorporate ftp into this script? ... (2 Replies)
Discussion started by: RApds
2 Replies

9. Shell Programming and Scripting

Using mail command to notify the status of script

Can someone please help me with this script, I'm trying to create system backup on AIX, for this I want to first mount the filesystem if it is not mounted, then create the backup and unmount the filesystem but I'm having problem while using the mail command to notify the status of filesystem... (9 Replies)
Discussion started by: mbak
9 Replies

10. Shell Programming and Scripting

How the first script should notify in case there is no response from second

Hi Experts, I am trying to write a ksh script that it should notify in case there is no response from the other script. I mean to say that I got a.sh and b.sh the execution of b.sh depends on a.sh, so if there is no response from a.sh, b.sh should notify me about the same. Thanks in Advance (4 Replies)
Discussion started by: rajusa10
4 Replies

Featured Tech Videos