If I create a file descriptor in bash and point it at a file and put some data into it:
exec 5>>file
for i in {1..10000}; do
echo "$i" >&5
done
File descriptor 5 holds open "file" and whenever I write data to that descriptor, it appends the data and keeps its current place in the file.
If I re-point that same descriptor to the same file again:
exec 5>>file
Does it close the current fd and recreate a new one pointing back to that same file? So the next time I write data, it has to find the end of the file before appending onto the end? Or, is it smart enough to realize the descriptor is already pointed to that file, and do nothing. What is going on at a low level?
edit: More specifically I am logging data to a file using file descriptors to hold the file open. Some relevant bits:
recv_udp () { # Receives a udp stream, timestamps and removes commas and the period from the seconds field, deletes blank lines.
socat -L "$LOCKFILE" -u udp-recv:"$UDP_IN",reuseaddr STDIO | ts '%Y %j %H %M %.S' | sed -u -e 's/,/ /g' -e 's/\./ /' -e '/^$/d' &
}
file_handle () { # Points file descriptor 9 to the desired log file
exec 9>>"$DATADIR"/"$CRUISEID"/"$STAMP"/"$STAMP"_"$JDY"_raw
}
parse_lci90i () { # Parse lci90i winch controller mtnw2 data stream.
echo "Started $STAMP $CRUISEID $(date +'%c')" >> "$LOGFILE"
while read YR JDY HR MIN SEC uS GARBAGE GARBAGE TENS SPD LINE CHKSUM; do
printf '%4.0f %03.0f %02.0f %02.0f %02.0f %.3s %s %.0f %.1f %.0f\n' "$YR" "$JDY" "$HR" "$MIN" "$SEC" "$uS" "$STAMP" "$SPD" "$LINE" "$TENS"
check_path
file_handle
printf '%4.0f %03.0f %02.0f %02.0f %02.0f %.3s %s %.0f %.1f %.0f\n' "$YR" "$JDY" "$HR" "$MIN" "$SEC" "$uS" "$STAMP" "$SPD" "$LINE" "$TENS" >&9
done
}
Socat reads a 20hz udp feed and pumps it into a parser via process substitution. You can see here that every time a line of data gets read, the file descriptor gets remade. So yes I am curious about whether or not this is any more efficient than just taking the stdout from the parser and just >> right into the log file.
Thanks for the replies so far, you are all pretty insightful. The filename of the data file contains the julian day from the data timestamp, so the log file rolls over every day when the date changes.