Discussion
Loading...

Post

Log in
  • About
  • Code of conduct
  • Privacy
  • About Bonfire
nixCraft 馃惂
nixCraft 馃惂
@nixCraft@mastodon.social  路  activity timestamp 2 weeks ago

At top of your bash script add this after `#!/bin/bash` line to aborts the script immediately and add robustness to your script
```
set -euo pipefail
```

Use the following to trap error and show line number where your script failed to debut at run time or development time:
```
_failed() {
echo "$0 - Script failed at line $1"
}

# Trap the ERR
trap '_failed $LINENO' ERR
```

See `man bash` or `help set` for more.

  • Copy link
  • Flag this post
  • Block
Federation Bot
Federation Bot
@Federation_Bot replied  路  activity timestamp 5 days ago

@nixCraft it reminds me of this https://share.google/xMSA8igtqWRG2mNqk

  • Copy link
  • Flag this comment
  • Block
Rihards Olups
Rihards Olups
@richlv@mastodon.social replied  路  activity timestamp 2 weeks ago

@nixCraft Ah, the global pipefail.
The indicator of a bash novice or slop-generated script...

Indiscriminate "set -euo pipefail" - especially in scripts that have no pipes whatsoever - is one of the best hints that somebody generated the script and did not understand it :)

https://mywiki.wooledge.org/BashPitfalls#pipefail

  • Copy link
  • Flag this comment
  • Block
S0AndS0
S0AndS0
@S0AndS0@mastodon.social replied  路  activity timestamp 2 weeks ago

@nixCraft I like setting `errtrace` and `functrace` too because, no matter the times I regret writing big Bash scripts, I end up writing big Bash scripts x-)

Is so bad that instead of an intervention, I wrote a big Bash function for formatting stack-trace-like output when my questionable code produces errors

Image of following questionable Bash code;

```bash
#!/usr/bin/env bash

##
# -e -> errexit -> Exit immediately if a command exits with a non-zero status
# -E -> errtrace -> ERR trap is inherited by shell functions
# -T -> functrace -> DEBUG and RETURN traps are inherited by shell functions
set -eET

##
# source: https://github.com/bash-utilities/trap-failure/
failure(){
    local -n _lineno="${1:-LINENO}"
    local -n _bash_lineno="${2:-BASH_LINENO}"
    local _last_command="${3:-${BASH_COMMAND}}"
    local _code="${4:-0}"
    local _line

    ## Workaround for read EOF combo tripping traps
    ((_code)) || {
      return "${_code}"
    }

    local -a _output_array=()
    _output_array+=(
        '---'
        "lines_history: [${_lineno} ${_bash_lineno[*]}]"
        "function_trace: [${FUNCNAME[*]}]"
        "exit_code: ${_code}"
    )

    _output_array+=( 'source_trace:' )
    for _line in "${BASH_SOURCE[@]}"; do
      _output_array+=( "  - ${_line}" )
    done

    _output_array+=( 'last_command: ->' )
    while read -r _line; do
      _output_array+=( "  ${_line}" )
    done <<< "${_last_command}"

    _output_array+=('---')
    printf >&2 '%s\n' "${_output_array[@]}"
    exit "${_code}"
}
trap 'failure "LINENO" "BASH_LINENO" "${BASH_COMMAND}" "${?}"' ERR

# ... Rest of questionable coding choices...
```
Image of following questionable Bash code; ```bash #!/usr/bin/env bash ## # -e -> errexit -> Exit immediately if a command exits with a non-zero status # -E -> errtrace -> ERR trap is inherited by shell functions # -T -> functrace -> DEBUG and RETURN traps are inherited by shell functions set -eET ## # source: https://github.com/bash-utilities/trap-failure/ failure(){ local -n _lineno="${1:-LINENO}" local -n _bash_lineno="${2:-BASH_LINENO}" local _last_command="${3:-${BASH_COMMAND}}" local _code="${4:-0}" local _line ## Workaround for read EOF combo tripping traps ((_code)) || { return "${_code}" } local -a _output_array=() _output_array+=( '---' "lines_history: [${_lineno} ${_bash_lineno[*]}]" "function_trace: [${FUNCNAME[*]}]" "exit_code: ${_code}" ) _output_array+=( 'source_trace:' ) for _line in "${BASH_SOURCE[@]}"; do _output_array+=( " - ${_line}" ) done _output_array+=( 'last_command: ->' ) while read -r _line; do _output_array+=( " ${_line}" ) done <<< "${_last_command}" _output_array+=('---') printf >&2 '%s\n' "${_output_array[@]}" exit "${_code}" } trap 'failure "LINENO" "BASH_LINENO" "${BASH_COMMAND}" "${?}"' ERR # ... Rest of questionable coding choices... ```
Image of following questionable Bash code; ```bash #!/usr/bin/env bash ## # -e -> errexit -> Exit immediately if a command exits with a non-zero status # -E -> errtrace -> ERR trap is inherited by shell functions # -T -> functrace -> DEBUG and RETURN traps are inherited by shell functions set -eET ## # source: https://github.com/bash-utilities/trap-failure/ failure(){ local -n _lineno="${1:-LINENO}" local -n _bash_lineno="${2:-BASH_LINENO}" local _last_command="${3:-${BASH_COMMAND}}" local _code="${4:-0}" local _line ## Workaround for read EOF combo tripping traps ((_code)) || { return "${_code}" } local -a _output_array=() _output_array+=( '---' "lines_history: [${_lineno} ${_bash_lineno[*]}]" "function_trace: [${FUNCNAME[*]}]" "exit_code: ${_code}" ) _output_array+=( 'source_trace:' ) for _line in "${BASH_SOURCE[@]}"; do _output_array+=( " - ${_line}" ) done _output_array+=( 'last_command: ->' ) while read -r _line; do _output_array+=( " ${_line}" ) done <<< "${_last_command}" _output_array+=('---') printf >&2 '%s\n' "${_output_array[@]}" exit "${_code}" } trap 'failure "LINENO" "BASH_LINENO" "${BASH_COMMAND}" "${?}"' ERR # ... Rest of questionable coding choices... ```
  • Copy link
  • Flag this comment
  • Block
grefeng
grefeng
@grefeng@mastodon.social replied  路  activity timestamp 2 weeks ago

@nixCraft Make the shebang portable with

```
#!/usr/bin/env bash
```

  • Copy link
  • Flag this comment
  • Block
Federation Bot
Federation Bot
@Federation_Bot replied  路  activity timestamp 2 weeks ago

@nixCraft

Writing this down for when I get back to the PC.

  • Copy link
  • Flag this comment
  • Block

A small Bonfire corner on the internet

This is a small personal instance of Bonfire in the Fediverse.

A small Bonfire corner on the internet: About 路 Code of conduct 路 Privacy 路
Bonfire social 路 1.0.1 no JS en
Automatic federation enabled
Log in
  • Explore
  • About
  • Code of Conduct