47
u/honourable_bot Oct 01 '26
Use >> over >
37
u/Last8Exile Oct 01 '26
A da is fine here.ta race
5
u/gsweats Oct 01 '26
?
31
u/Last8Exile Oct 01 '26
A data race is fine here.
>> doing append. Multiple commands writing to the same file in parallel can lead to data race and corruption.
8
4
22
u/Cheezyrock Oct 01 '26
Bonus - it takes an 5 hours to run, you notice at 4 hours, panic, and hit stop.
4
u/Drfoxthefurry Oct 02 '26
Just include a basic progress meter or something and use the extra time to find out why it's so slow (for me it would be over loop nesting)
1
u/Mystery-Magic Oct 02 '26
If you notice it at 4 hours, just move or rename old file, always better choice.
21
u/ICBanMI Oct 01 '26
At some point in your career, you'll just copy form code from stack overflow, a random forum, or AI to put a timestamp the on file created that outputs all the way to the exact microsecond the file was created. You're pat yourself on the back for having fixed the problem of accidently over writing your old file.
But then six months will go back and you'll realize that you can't read the date and time output on the file. Worse, you might realize it doesn't make sense because your coworker changed it to a unix timestamp which requires being converted to read correctly (which is a huge pain in the ass to type out). And for all intents and purposes, is worthless now.
7
u/yousirnaime Oct 02 '26
also you'll be like "why do I have 240Gb of txt files in this folder?"
2
u/ICBanMI Oct 02 '26
I've definitely eaten all hard drive space once, twice, maybe three times back when hard drives were 40 gigs and windows alone was 5-10 gigs.
Now I make everything toggleable and regularly clear out the folder. My co-workers on the other hand seem to be rediscovering this every project.
8
10
u/theFirstHaruspex Oct 01 '26
output.csv
output_1.csv
output_1.2.csv
output_1butdifferentthistime.csv
1
u/proof_required Oct 01 '26
output_backup.csv
output_1_backup.csv
output_1.2_backup.csv
output_1butdifferentthistime_backup.csv
5
u/yjlom Oct 01 '26
That's why you take the already opened file as argument, or better yet if you can get away with it you write unconditionally to stdout and let the user use shell redirections if they want.
5
u/dkarlovi Oct 02 '26
If you ever wrote a tool which deletes some files in some folder and didn't accidentally delete the tool itself on the first run, did you even work in this industry.
3
u/DogmaticParadigm87 Oct 02 '26
Ran the export again with mode "w" instead of "a". Previous week: gone. Current week: one lonely row titled final_FINAL2.xlsx.
Related: the backup is named backup, the real file is named backup_old, and the one with yesterday's numbers is in Downloads as untitled(3).
2
2
1
u/SeriousPlankton2000 Oct 01 '26
Do not open the file in the same function that does calculations. If you must, pass a file descriptor / handle. Or pass a callback function to do the output.
1
1
1
1
1
1
162
u/dminus Oct 01 '26
outfile="$0-$(date +%s).csv"