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


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Solaris KSH shell script to copy all lines from one file to another
# 1  
Question 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
# 2  
So you just need to copy whole file? Just do:
Code:
cp /your/file /var/tmp/your_file

and when you need to recover it:
Code:
cp /var/tmp/your_file /your/file

# 3  
a little different than that

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!
# 4  
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:
Code:
#!/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...
# 5  
Thanks!

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

-zig
# 6  
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.

Code:
#!/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

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #898
Difficulty: Medium
The Toybox project was started in 2012 and became a BSD-licensed BusyBox alternative.
True or False?

10 More Discussions You Might Find Interesting

1. Ubuntu

How to lock a file through UNIX KSH shell script?

I wrote two shell scripts in UNIX that renames the same file and scheduled them at the same time. The following are the steps that I followed:- 1. I wrote 2 scripts named s1.sh and s2.sh, both trying to add exec_ prefix to the name of the files present in a folder i which already dont start... (4 Replies)
Discussion started by: piuli
4 Replies

2. Shell Programming and Scripting

ksh shell scripting to copy a file

Hi. I am a new Unix admin and I've been tasked to write a ksh script that copies my .profile into my /home directory on all servers. I'm new to this and having a difficult time scripting it. Any ideas? (6 Replies)
Discussion started by: david_tech
6 Replies

3. Shell Programming and Scripting

Shell script to copy particular file from directories recursively

I have directory path in which there are several sub directories. In all these sub dir there will be one env.cnf file. I want to copy this env.cnf file from each sub dir's and place them in destination path by creating same filename as sub dir_env.cnf. After copying env.cnf files from source... (4 Replies)
Discussion started by: Optimus81
4 Replies

4. UNIX for Dummies Questions & Answers

How to use sed to copy specific lines from a file using shell variables?

hello! I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines? if the input_file is something like this and if the following is the script a=2 b=4... (4 Replies)
Discussion started by: a_ba
4 Replies

5. Shell Programming and Scripting

Shell script to copy file

Dear all, I have a database with thousands of files with the structure of name is: Filename_hour_year.abc Filename_hour_year_1.abc .............. So what I need is how to write a script that all file with contain the character "_1" will copy to "_2" For example: file name:... (7 Replies)
Discussion started by: hainguyen1402
7 Replies

6. Shell Programming and Scripting

KSH SHELL: problem calculation number of lines inside compressed file

Hi Gurus, I am working with a korn shell script to simplify some operations of calculation number of lines inside compressed file. The called function (inside a cycle) is the following: ######################################### # F.ne: CheckCount #########################################... (3 Replies)
Discussion started by: GERMANICO
3 Replies

7. Shell Programming and Scripting

Shell Script - Copy File at intervals

Hi, I want to copy some files from a Folder say, /usr/X at random intervals to another location. Basically, new files will be dumped at random intervals to location /usr/X and I have to copy those new files to some other location (after copying, I cannot delete those files from source... (2 Replies)
Discussion started by: angshuman_ag
2 Replies

8. Shell Programming and Scripting

Help with a shell script to modify one line and copy the next 9 to same file

Hi everyone, the problem is quite simple, yet I can't find an easy solution using awk. I need to search for a string in $3, then if I find this string, copy the line,modify $3, and copy the next 9 lines to the same file. My problem is in the copying of the lines... Finding and modifying... (5 Replies)
Discussion started by: Teroc
5 Replies

9. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies

10. Shell Programming and Scripting

Script to capture new lines in a file and copy it to new file

Hi All, I have a file that gives me new line/output every 5 minutes. I need to create a script that capture new line/output besides "IN CRON_STATUS", in this case the new output is "begin ------ cron_status.sh - -----------". I want this script to capture the line starting from "begin ------... (0 Replies)
Discussion started by: fara_aris
0 Replies

Featured Tech Videos