Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC2002: when cat is on a line on its own, suggest putting the redirection before the command instead #1640

Open
pabs3 opened this issue Jul 14, 2019 · 0 comments

Comments

@pabs3
Copy link

commented Jul 14, 2019

For bugs

  • Rule Id: SC2002
  • My shellcheck version: 0.5.0

Here's a snippet or screenshot that shows the problem:

#!/bin/sh
cat file |
tr '[:cntrl:]' '?' |
sort |
uniq -c

Here's what shellcheck currently says:

In foo.sh line 2:
cat file |
    ^-- SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.

Here's what I wanted or expected to see:

In foo.sh line 2:
cat file |
tr '[:cntrl:]' '?' |
    ^-- SC2002: Useless cat. Consider this instead:
< file \
cmd |

Comments

Having the filename first is more aesthetically pleasing, especially with multi-line pipelines, which is why many people use cat, but they don't know about "< file cmd" redirections so then end up with useless-use-of-cat.

I'm not sure about the multi-line suggestion, but I think a quoted string would be more confusing due to the line continuation character and then escaped newline: "< file \ncmd".

The rule's wiki page does put the filename before the command in one case, but doesn't have a multi-line case and the suggestions ShellCheck produces for SC2002 should reflect the multi-line case.

I found some discussion about this on StackOverflow.

I can't check shellcheck.net because it uses JavaScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.