From 875fc27dd9dc3e6b81b450639ab5c789c4a99ed2 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 15 Apr 2017 19:16:37 -0700 Subject: [PATCH] Created SC2210 (markdown) --- SC2210.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 SC2210.md diff --git a/SC2210.md b/SC2210.md new file mode 100644 index 0000000..5339c74 --- /dev/null +++ b/SC2210.md @@ -0,0 +1,34 @@ +## This is a file redirection. Was it supposed to be a comparison or fd operation? + +### Problematic code: + +```sh +if x > 5; then echo "true"; fi +``` +or +```sh +foo > /dev/null 2>1 +``` + +### Correct code: + +```sh +if (( x > 5 )); then echo "true"; fi +``` +or +```sh +foo > /dev/null 2>&1 +``` +### Rationale: + +You are redirecting to or from a filename that is an integer. For example, `ls > file` where `file` happens to be `3`. + +This is not likely to be intentional. The most common causes are: + +1. Trying to compare two numbers, as in `x > 5`. This should instead be `[ "$x" -gt 5 ]` or `(( x > 5 ))`. +1. Trying similarly to compare command output, as in `grep -c foo file > 100` instead of `[ "$(grep -c foo file)" -gt 100 ]` +1. Malformed FD operations, such as writing `1>2` instead of `1>&2`. + +### Exceptions: + +None. If you do want to create a file named `4`, you can quote it to silence shellcheck and make it more clear to humans that it's not supposed to be taken numerically. \ No newline at end of file