I need to write a script to find out if there are any .c files created/removed from the last time i monitored the files available. i first created a file to contain all the .c files available on the system. (ls *.c > file1)
I created another file using the same command.
I used the comm [-123] file1 file2 command to compare the differences.
Now i need to display the result in user readable form
e.g. These files has bee removed
file1.c
file2.c
These files have been created
file3.c
file4.c
OR
No files has been created/removed at all
doesn't seems to work. read the manual and understood what the awk command does, but ain very sure how it works. also i don't quite understand the 3rd line. i see that you uses the command like print and the way you uses the for loop. just need to clarify, can i use these command in the shell script? cause usually i used the echo command to print a message on the screen. I am running under the bourne shell or tcsh.
Take a look at this. Using diff and its <, > change properties.
#! /bin/sh
# rt.sh
if [[ $# -ne 2 ]] ; then
echo "Need just 2 files to compare"
exit 1
fi ;
# Take the diff by ignoring the blank and whitespaces.
diff -b -w ${1} ${2} > ${1}.diff
if [[ $? -eq 0 ]] ; then
echo "No files were added/removed"
else
echo "Files were added/removed"
fi ;
# Process the diff file.
# A line might look like
# < text-which-went-out
# > text-which-came-in
while read line
do
if [[ ${line:0:2} == "< " ]] ; then
echo ${line:2} is removed.
fi ;
if [[ ${line:0:2} == "> " ]] ; then
echo ${line:2} is added.
fi ;
done < ${1}.diff
rm ${1}.diff
Run it as ./rt.sh file1 file2
Remember the old file file1 should always be the first argument. Else, the logic would reverse.