Sponsored Content
Full Discussion: File name with yyyymmddhhmi
Top Forums UNIX for Beginners Questions & Answers File name with yyyymmddhhmi Post 303019989 by rbatte1 on Wednesday 11th of July 2018 01:30:25 PM
Old 07-11-2018
You also need to ensure that your file creation and all the output being put into it is Atomic, i.e. a single operation that takes a single IO. Often this is done by creating the file and building it up with content as another name and then when it is ready, renaming it to it appears as a complete file instantly.

If you have a file that takes even a whole second to build, then you may sometimes be unlucky and fetch a partial file.

Something like this would illustrate it:-
Code:
((random_delay=$RANDOM%9))

( sleep $random_delay ; cat /tmp/myfile ) &      # Display the file at some undetermined time

date                             > /tmp/myfile
echo "First data line of file"  >> /tmp/myfile
sleep 3
echo "Second data line of file" >> /tmp/myfile
sleep 3
echo "Last data line of file"   >> /tmp/myfile
wait                                             # In case the random delay is long, wait for the background sleep/cat to finish

If you run this repeatedly, you may get 1, 2 or 3 lines of output. You can compensate by the cat reading a file that is always the finished article, so more like:-
Code:
((random_delay=$RANDOM%9))

( sleep $random_delay ; cat /tmp/finalfile ) &   # Display the file at some undetermined time

date                             > /tmp/myfile
echo "First data line of file"  >> /tmp/myfile
sleep 3
echo "Second data line of file" >> /tmp/myfile
sleep 3
echo "Last data line of file"   >> /tmp/myfile

mv /tmp/myfile /tmp/finalfile                    # Atomic file write
wait                                             # In case the random delay is long, wait for the background sleep/cat to finish

It may fail first time if the file does not exist, but afterwards it would only ever read a completed file.

Doing this, you could also make a link as a "latest" file, so you always know which one to pick up.


I hope that this helps,
Robin
 

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies
rmvb(9F)						   Kernel Functions for Drivers 						  rmvb(9F)

NAME
rmvb - remove a message block from a message SYNOPSIS
#include <sys/stream.h> mblk_t *rmvb(mblk_t *mp, mblk_t *bp); INTERFACE LEVEL
Architecture independent level 1 (DDI/DKI). PARAMETERS
mp Message from which a block is to be removed. mblk_t is an instance of the msgb(9S) structure. bp Message block to be removed. DESCRIPTION
The rmvb() function removes a message block (bp) from a message (mp), and returns a pointer to the altered message. The message block is not freed, merely removed from the message. It is the module or driver's responsibility to free the message block. RETURN VALUES
If successful, a pointer to the message (minus the removed block) is returned. The pointer is NULL if bp was the only block of the message before rmvb() was called. If the designated message block (bp) does not exist, -1 is returned. CONTEXT
The rmvb() function can be called from user, interrupt, or kernel context. EXAMPLES
This routine removes all zero-length M_DATA message blocks from the given message. For each message block in the message, save the next message block (line 10). If the current message block is of type M_DATA and has no data in its buffer (line 11), then remove it from the message (line 12) and free it (line 13). In either case, continue with the next message block in the message (line 16). 1 void 2 xxclean(mp) 3 mblk_t *mp; 4 { 5 mblk_t *tmp; 6 mblk_t *nmp; 7 8 tmp = mp; 9 while (tmp) { 10 nmp = tmp->b_cont; 11 if ((tmp->b_datap->db_type == M_DATA) && (tmp->b_rptr == tmp->b_wptr)) { 12 (void) rmvb(mp, tmp); 13 freeb(tmp); 14 } 15 tmp = nmp; 16 } 17 } SEE ALSO
freeb(9F), msgb(9S) Writing Device Drivers STREAMS Programming Guide SunOS 5.11 16 Jan 2006 rmvb(9F)
All times are GMT -4. The time now is 05:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy