Skip to main content
added 78 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

A simpler way to read the first two lines from a command

Instead of:

lver=$(code --version | head -n 2 | tail -n 1)
lnam=$(code --version | head -n 1)

You could use read:

{ read -r lnam; read -r lver; } < <(code --version)

Alternatively, using an array:

version_info=($(code --version))
lnam=${version_info[0]}
lver=${version_info[1]}

Exit with non-zero code in case of failures

Exit with non-zero code in case of failures

A simpler way to read the first two lines from a command

Instead of:

lver=$(code --version | head -n 2 | tail -n 1)
lnam=$(code --version | head -n 1)

You could use read:

{ read -r lnam; read -r lver; } < <(code --version)

Alternatively, using an array:

version_info=($(code --version))
lnam=${version_info[0]}
lver=${version_info[1]}

Exit with non-zero code in case of failures

added 78 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

It's a pretty nice script, I like it. Special thanks for checking the script with shellcheck!

The script does exit 0 at multiple places when it detects that something is off. The recommended practice is to exit with a non-zero code to signal that the execution was a failure. Using exit 1 is fine solution.

Use set -euo pipefail to fail fast on errors

A common mistake in scripts is forgetting to check that some important command runs successfully, and by default Bash will continue running the rest of the script, which may cause severe harm.

A good simple safeguard is to include set -euo pipefail at the top of the script, which will make Bash abort on the first failure, and also treat referencing unassigned variables as an error, among other things. For more details see help set and man bash.

Minor nits

echo "" is really the same as echo, no need for the empty string parameter.

In a command like curl -s "long url" -o "short value" I prefer to rearrange the terms as curl -s -o "short value" "long url" to keep the most important part of the command visible without horizontal scrolling. If anything important goes at the far right end of a line, there is a higher risk of overlooking it.

instdir, checkdir, and checkres are constants with static values, therefore it's good to group them together at the top of the script.

It's a pretty nice script, I like it.

The script does exit 0 at multiple places when it detects that something is off. The recommended practice is to exit with a non-zero code to signal that the execution was a failure. Using exit 1 is fine solution.

It's a pretty nice script, I like it. Special thanks for checking the script with shellcheck!

The script does exit 0 at multiple places when it detects that something is off. The recommended practice is to exit with a non-zero code to signal that the execution was a failure. Using exit 1 is fine solution.

Use set -euo pipefail to fail fast on errors

A common mistake in scripts is forgetting to check that some important command runs successfully, and by default Bash will continue running the rest of the script, which may cause severe harm.

A good simple safeguard is to include set -euo pipefail at the top of the script, which will make Bash abort on the first failure, and also treat referencing unassigned variables as an error, among other things. For more details see help set and man bash.

Minor nits

echo "" is really the same as echo, no need for the empty string parameter.

In a command like curl -s "long url" -o "short value" I prefer to rearrange the terms as curl -s -o "short value" "long url" to keep the most important part of the command visible without horizontal scrolling. If anything important goes at the far right end of a line, there is a higher risk of overlooking it.

instdir, checkdir, and checkres are constants with static values, therefore it's good to group them together at the top of the script.

added 78 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

GettingGet raw values from jq output using -r

This is pretty hacky, and inefficient way to strip the enclosing double-quotes from values in the output of jq, using multiple processes:

A much simpler solution is possible thanks to the -r or --raw option of jq-output flag:

Exit with non-zero code in case of failures

The script does exit 0 at multiple places when it detects that something is off. The recommended practice is to exit with a non-zero code to signal that the execution was a failure. Using exit 1 is fine solution.

Getting raw values from jq

This is pretty hacky, and inefficient, using multiple processes:

A much simpler solution is possible thanks to the -r or --raw option of jq:

Get raw values from jq output using -r

This is hacky and inefficient way to strip the enclosing double-quotes from values in the output of jq, using multiple processes:

A much simpler solution is possible thanks to the -r or --raw-output flag:

Exit with non-zero code in case of failures

The script does exit 0 at multiple places when it detects that something is off. The recommended practice is to exit with a non-zero code to signal that the execution was a failure. Using exit 1 is fine solution.

Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396
Loading

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant