
ShellCheck SC2181 Check Exit Code Directly Explained
Fix ShellCheck SC2181: test a command's exit status directly with if cmd or if ! cmd instead of checking $? afterwards, and when $? is still right.
Tag
39 posts

Fix ShellCheck SC2181: test a command's exit status directly with if cmd or if ! cmd instead of checking $? afterwards, and when $? is still right.

Fix user is not in the sudoers file: add the user to the sudo or wheel group as root, use su or recovery mode, and check sudoers syntax with visudo.

Fix grep and sed's invalid preceding regular expression error: a quantifier with nothing to repeat, BSD versus GNU differences, braces and macOS quirks.

Debug unexpected pipe output: stdio buffering with a pipe, stdbuf and unbuffer, python -u, grep --line-buffered, and interleaved stderr.

Fix SSH Connection timed out: tell it from refused, trace the path with nc and traceroute, check firewalls and security groups, and set ConnectTimeout.

Fix SSH connection refused: confirm sshd is running and listening, the port and address, firewall rules, and how it differs from a timeout.

Fix SSH Permission denied (publickey): read ssh -v output, check the key, the agent, file permissions on both sides, the user name and sshd logs.

Fix the Go error exec executable file not found in PATH: why exec.Command needs the program on PATH, LookPath, absolute paths, containers and services.

Fix PostgreSQL peer authentication failed for user: pg_hba.conf rules, Unix socket versus TCP, matching OS and database user names, and a safe change.

Fix ShellCheck SC2046: quote command substitutions to prevent word splitting, with ls, find and git examples, and safer arrays and mapfile patterns.

Fix ShellCheck SC2155: why local x=$(cmd) hides the command's exit status, and how to declare and assign on separate lines so set -e and checks still work.

Fix Jenkins script returned exit code 127: command not found, agent PATH differences, missing tools, Windows line endings and wrong interpreters.