0

I have a bash script that calls another script and I capture the stdout. If there is an error, I want to print the result. However; only the first word of the child script is displayed?

error_return() {
    echo "Error received during '$1' execution. Log:"
    while IFS= read line; do
        echo " $line "
    done <<< $2
}

build_doc() {
    echo "Building documentation"
    docbuild=$(bin/doc-build.sh)
    wait
    if [ $? -eq 0 ]; then
        echo "Documentation built."
    else
        error_return "build_doc" $docbuild
    fi
}

What do I do to show all the result text?

Frank C.
  • 7,758
  • 4
  • 35
  • 45
  • `"$docbuild"` needs to be in quotes. Right now the first word is in $2, the second one in $3, etc. This is a problem http://shellcheck.net/ will point out automatically. – Charles Duffy Nov 30 '22 at 14:05
  • 1
    BTW, there's no point to `wait` -- it only does anything when there are background tasks. And in general, testing `$?` is a bad idea. `if docbuild=$(bin/doc-build.sh); then echo "Documentation built."; else error_return build_doc "$docbuild"; fi` is the better practice. – Charles Duffy Nov 30 '22 at 14:06
  • BTW, putting logs on stdout (as this code assumes your docbuild command does) is not good practice; POSIX-compliant tools put logs, errors, and other diagnostic information on stderr, and keep stdout free for _actual output_. Similarly, that means your `echo`s of human-targeted content should have `>&2` on them. – Charles Duffy Nov 30 '22 at 14:07
  • Anyhow -- [Why is testing $? to see whether a command succeeded or not an antipattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – Charles Duffy Nov 30 '22 at 14:08
  • (also, think about using `bash -x yourscript` to trace operation when testing; that would have shown you all the words from the message being passed as separate arguments). – Charles Duffy Nov 30 '22 at 14:11
  • @CharlesDuffy - Thanks, I've restructured as you've indicated and works like a charm. – Frank C. Nov 30 '22 at 14:41

0 Answers0