org.sonar.l10n.shellcheck.rules.shellcheck.SC2094.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-shellcheck-plugin Show documentation
Show all versions of sonar-shellcheck-plugin Show documentation
ShellCheck plugin for SonarQube
SC2094 Make sure not to read and write the same file in the same pipeline.
Problematic code
grep foo file.txt | sed -e 's/foo/bar/g' > file.txt
Correct code
grep foo file.txt | sed -e 's/foo/bar/g' > tmpfile && mv tmpfile file.txt
Rationale
Each step in a pipeline runs in parallel.
In this case, grep foo file.txt
will immediately try to read file.txt
while sed .. > file.txt
will immediately try to truncate it.
This is a race condition, and results in the file being partially or (far more likely) entirely truncated.
Exceptions
You can ignore this error if:
- The file is a device or named pipe. These files don't truncate in the same way.
- The command mentions the filename but doesn't read/write it, such as
echo log.txt > log.txt
.
Additional Resources
© 2015 - 2025 Weber Informatics LLC | Privacy Policy