Register
It is currently Fri Oct 24, 2014 8:46 pm

bash script to notify when ever any files are changed


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Aug 07, 2013 12:30 pm   

Joined: Wed Aug 07, 2013 12:23 pm
Posts: 2
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

## 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>


Top
 Profile  
 PostPosted: Wed Aug 07, 2013 12:33 pm   

Joined: Wed Aug 07, 2013 12:23 pm
Posts: 2
## 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


Top
 Profile  
 PostPosted: Thu Aug 08, 2013 11:07 am   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 47
check out
http://www.ibm.com/developerworks/linux ... index.html
for some ideas with inotify.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP