How to store standard error in a variable

后端 未结 18 2368
难免孤独
难免孤独 2020-11-22 12:46

Let\'s say I have a script like the following:

useless.sh

echo \"This Is Error\" 1>&2
echo \"This Is Output\" 

And I have an

18条回答
  •  佛祖请我去吃肉
    2020-11-22 13:23

    For error proofing your commands:

    execute [INVOKING-FUNCTION] [COMMAND]
    

    execute () {
        function="${1}"
        command="${2}"
        error=$(eval "${command}" 2>&1 >"/dev/null")
    
        if [ ${?} -ne 0 ]; then
            echo "${function}: ${error}"
            exit 1
        fi
    }
    

    Inspired in Lean manufacturing:

    • Make errors impossible by design
    • Make steps the smallest
    • Finish items one by one
    • Make it obvious to anyone

提交回复
热议问题