Results 1 to 8 of 8
I have been made linux admin for a bunch of servers and I am still fairly green so i need some help.
Everynight, a series of files are written into ...
- 02-23-2010 #1Just Joined!
- Join Date
- Feb 2010
- Posts
- 5
Help with scripting
I have been made linux admin for a bunch of servers and I am still fairly green so i need some help.
Everynight, a series of files are written into a directory, they need to be moved to another location and a change log has to be maintained.
I am afraid I have no idea how to do this.
I am supposed to create a log file that looks like this
<filename>-datetimestamp.log where the datetimestamp is the last time the file was changed.
And the log should be formatted like this:
date time stamp
--------------------
files changes or added
date time stamp
--------------------
files changed or added
Also if no changes were made, don't make an entry.
Can this even be done?
- 02-23-2010 #2
Your problem is fairly ambiguous.
Are you writing a logfile for each file that was written into the directory? Or one logfile that records the behaviour of every file written? An example would also be helpful.
You can find the modification timestamp of a file by using the stat command: see "man 1 stat" for details on that.DISTRO=Arch
Registered Linux User #388732
- 02-23-2010 #3Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
And to add to Cabhan's post, there are plenty of Bash docs/guides/tutorials available.
This is one of the most common/useful - Bash Scripting
- 02-24-2010 #4Just Joined!
- Join Date
- Feb 2010
- Posts
- 5
I am writing one logfile. It needs to be appended. I don't mean to be ambiguous and I have looked a t alot of sites on scripting but there is an assumed level of knowledge on a lot of them that I find frustrating. I have been working in a windows environment for years and never had to script things so I am starting from 0 here.
I think I have a solution though.
This is what I am trying.
#/bin/sh
#Changelog Filename
log="Changelog.txt"
#Source Directory
source_dir="/source"
#Destination Directory
dest_dir="/dest"
#Set Date Format for timestamp
date=`date`
# look for empty dir
if [ "$(ls -A $source_dir)" ]; then
#source_dir is not empty, updates were available
echo "-------------------------------" >> $log
echo $date >> $log
ls $source_dir >> $log
echo "" >> $log
mv $source_dir/* $dest_dir
else
#source_dir is empty
touch $log
fi
Is this a good workable approach?
- 02-24-2010 #5
That looks pretty good! I have some suggestions for you, though.
First of all, Bash is kind of stupid when it comes to variables. For instance, if the value of $source_dir and $dest_dir are "/source" and "/dest", like you have, then:
becomesCode:mv $source_dir/* $dest_dir
However, if it was to be "/source directory" and "/dest directory" (note the spaces), then the line becomes:Code:mv /source/* /dest
This line would move a file called /source, all of the files in the subdirectory directory/ of the CURRENT directory, and a file called /dest into the subdirectory directory/ of the CURRENT directory.Code:mv /source directory/* /dest directory
The way to get around this is a basic rule: in Bash, ALWAYS quote your variables. Therefore, the line
should becomeCode:mv $source_dir/* $dest_dir
Note the quotes.Code:mv "$source_dir"/* "$dest_dir"
Some other notes:
Your test for the existence of files in the directory may work, but it's not particularly intuitive. I might suggest this instead:
The difference is that this explicitly counts the files in the directory instead of just checking to see if ls outputs anything.Code:if [ "$(ls -A1 "$source_dir" | wc -l)" -neq 0 ]; then # not empty # ... fi
Finally, I'm confused about your use of the "touch" in the else statement. The touch just updates the last modified time of the changelog, but you're not actually modifying it.DISTRO=Arch
Registered Linux User #388732
- 02-24-2010 #6Just Joined!
- Join Date
- Feb 2010
- Posts
- 5
thanks for the tips.
As for the touch, that was a requirement given to me... update the log time even if no changes made
- 02-25-2010 #7Just Joined!
- Join Date
- Feb 2010
- Posts
- 5
Ok a little more help if I can get it. The script I am working on is below.
#/bin/sh
#Changelog Filename
log="/<log-name>.txt"
#Cache Directory
cache_dir="/<cache directory>"
#REPO Directory
repo_dir="/<repodirectory>"
#Copy Destination
dest_dir="/<destination>"
#Set Date Format for timestamp
date=`date`
# look for empty dir
if [ "$(ls -A1 "$cache_dir" | wc -l)" -neq 0 ]; then
#cache_dir is not empty, updates were available
echo "-------------------------------" >> $log
echo $date >> $log
ls -R $cache_dir >> $log
echo "" >> $log
rsync -avz --progress $cache_dir/* $repo_dir
rm -rf "$cache_dir"/*
else
#cache_dir is empty
touch $log
# Copying files over to fileserver
rsync -avz --progress --delete --exclude=citrix/ $repo_dir/* *$dest_dir*
rsync -avz --progress --delete $log $dest_dir
echo "finished"
fi
everything seems to be working fine except the rm command to clear the cache directory. what am I doing wrong here?Last edited by pkalbach; 02-25-2010 at 01:39 PM. Reason: wrong version of script add
- 02-25-2010 #8Just Joined!
- Join Date
- Feb 2010
- Posts
- 5
Ok, I posted the wrong version and it didn't let me change it...
#/bin/sh
#Changelog Filename
log="/<log-name>.txt"
#Cache Directory
cache_dir="/<cache>"
#REPO Directory
repo_dir="/<repo>"
#Copy Destination
dest_dir="/<dest>"
#Set Date Format for timestamp
date=`date`
# look for empty dir
if [ "$(ls -A1 "$cache_dir" | wc -l)" -neq 0 ]; then
#cache_dir is not empty, updates were available
echo "-------------------------------" >> "$log"
echo "$date" >> "$log"
ls -R "$cache_dir" >> "$log"
echo "" >> "$log"
rsync -avz --progress "$cache_dir"/* "$repo_dir"
sleep 20
rm -rf "$cache_dir"/*
else
#rpm_dir is empty
touch $log
# Copying files over to file server
rsync -avz --progress --delete --exclude=citrix/ "$repo_dir"/* "$dest_dir"
rsync -avz --progress --delete "$log" "$dest_dir"
echo "finished"
fi


Reply With Quote