Skip to main content
2 of 3
added 42 characters in body
muru
  • 73.8k
  • 14
  • 203
  • 302

ShellCheck does not detect error? How to fix my snippet?

I likely have syntax errors in the following (POSIX) shell snippet:

#!/bin/sh
if tput setaf >/dev/null 2>&1; then tput_init_linux else tput_init_none;
elif tput AF  >/dev/null 2>&1; then tput_init_bsd   else tput_init_none;
else tput_init_none; fi

ShellCheck 0.10.0 does say nothing about it, I found it basically by accident.

I tested as follows:

  1.  if tput setaf >/dev/null 2>&1; then tput_init_linux else tput_init_none; fi
    

    this went ok: tput_init_linux: command not found

  2. Reverting the logic with !:

    if ! tput setaf >/dev/null 2>&1; then tput_init_linux else tput_init_none; fi
    

    says nothing, but if I add one semicolon:

  3.  if ! tput setaf >/dev/null 2>&1; then tput_init_linux; else tput_init_none; fi
    

    it appears to work as expected: tput_init_none: command not found


But I seem to be unable to chain these multiple if-statements afterward.


So as you can see, I reached a state, where one statement works, but chaining it seems problematic. Can anyone help?

Vlastimil Burián
  • 29k
  • 62
  • 196
  • 329