Suppose that there is a task in which a file needs to be created and written to only if the path that the file is to reside at does not already exist (this means that if the desired path is already a directory, symbolic link, or anything else, the task fails). The simplest way to do this in bash is this way:
if [[ -e "$file_path" || -L "$file_path" ]]
then
# Either throw an error, exit the script,
# or something like that here.
fi
output_the_data > "$file_path"
The problem here is that another process could create something at the path $file_path inbetween the test and output_the_Data > "$file_path". I already know that a native program, or a script written in a language that can interface with native libraries (e.g., Python), can probably rely on features of Linux's file IO-related system calls—or Windows' system calls, if we're running bash on Windows, I guess—to avoid a race condition here. For instance, maybe it would be done through some atomic check-and-create mode or maybe it opens a file descriptor and tests are performed on that file descriptor (e.g., to determine if the path existed prior to opening the descriptor). In the latter case, if the file gets deleted while it's being used by the process that creates it, then it leads to no error because it's name is simply unlinked from that path.
I considered checking to see if tee offers some fail-if-the-path-already-exists mode; it does not. So, restricting this question to only the case of where one is using a bash environment, how can one avoid the above race condition?