sh: syntax: keep "exit guards" in place

https://docs.fedoraproject.org/en-US/Fedora_Security_Team/1/html/Defensive_Coding/sect-Defensive_Coding-Shell-Edit_Guard.html recommends to have main "$@" ; exit $? as single line… Maybe it’s worth to support this case and avoid auto-rewriting it as two lines?

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 19 (9 by maintainers)

Most upvoted comments

This specific code can be rewritten as

die() {
    echo "$*" >&2
    exit 1
}

try() {
    "$@" || die "failed: $*"
}

try test -f files/SHA256SUMS
or
test -f files/SHA256SUMS || die "files/SHA256SUMS is missing"

This issue is about guarding against in-place edits while script is running.