Solaris KSH shell script to copy all lines from one file to another

Hello, more of a windows wscript guy. However I took a new position that requires me to support some solaris servers. So... issue is that I need to copy all lines from a file to a temporary file and then copy them back into the original file starting at line 1. Reason I need to do this is because if the process is stopped and then started I lose all of the log data prior to stopping the process so I need to preserve what was in the log file.

thanks!
-Zig

So you just need to copy whole file? Just do:

cp /your/file /var/tmp/your_file

and when you need to recover it:

cp /var/tmp/your_file /your/file

copying the file to the tmp file is only one of the steps. The issue is that after the process is started it creates it's own file (overwriting the original) so what I need to do via the shell script is copy all the lines form the tmp file back into the file created by starting the process. Also, I need to programatically copy them back in starting at line 1 and not at the bottom of the file.

thanks!

Copying file from /tmp will replace the old file, so it will do exactly what you want, which is copy all the lines starting from the first. You can copy the file before process starts, and copy it back right after it is started, like that:

#!/bin/sh
cp /your/file /var/tmp/your_file
cmd_to_start_process
cp /var/tmp/your_file /your/file

Of course it won't work if your process holds lock on that file...

Thanks alot. That worked just like I needed it to. Nice and simple too.

-zig

The only problem with that solution is that if there are any log entries that are created during the application startup, they'll be lost when you copy the file back on top. You may also want to mv the file from /var/tmp to keep things tidy.

This should keep your last four logs while maintaining the current startup log.

#!/bin/sh
if [ -f /your/file.2 ]
then
  mv /your/file.2 /your/file.3
fi
if [ -f /your/file.1 ]
then
  mv /your/file.1 /your/file.2
fi
if [ -f /your/file.0 ]
then
  mv /your/file.0 /your/file.1
fi
mv /your/file /your/file.0
cmd_to_start_process