W
W
Warfare Noise2014-02-28 16:11:51
linux
Warfare Noise, 2014-02-28 16:11:51

How to redirect stderr to bash via a function?

Good day,
There is, for example, a piece of code like this:

#! /bin/bash

error_action () {
    echo "Failed! Check $ERR_LOG" 
    exit 1
}

apt-get install -y build-essential linux-headers-$(uname -r) >/dev/null || error_action

How to implement redirection of stderr output to the log through the function, if the previous command ends with an error? In this case, the result of executing the command from the function should be displayed on stdout echo, and nothing more.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
Warfare Noise, 2014-02-28
@powerthrash

This option is not suitable. The matter is that the redirection has to be in function. The script is quite voluminous, it 2>/tmp/error.logwould not be entirely rational to write it separately for all commands.

E
egor_nullptr, 2014-02-28
@egor_nullptr

... 1>/dev/null 2>/tmp/error.log || error_action

I
Ivan Starkov, 2014-02-28
@icelaba

see set -o pipefail

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question