DEV Community

classx
classx

Posted on

Disable bash script output, but save to file log

Option 1

#!/bin/bash
sudo apt install -y package_name | tee -a /tmp/app.log >/dev/null
sudo apt install -y package_name1 | tee -a /tmp/app.log >/dev/null
sudo apt install -y package_name2 | tee -a /tmp/app.log >/dev/null

Enter fullscreen mode Exit fullscreen mode

Option 2

disable all output to tty but save to file

#!/bin/bash
exec 1> >(tee -a /tmp/app.log >/dev/null)
sudo apt install -y package_name
sudo apt install -y package_name1
sudo apt install -y package_name2
Enter fullscreen mode Exit fullscreen mode

Option 3

enable all output to tty and save to file

#!/bin/bash
exec 2> >(tee -a /tmp/app.log >&2) > >(tee -a /tmp/app.log")
sudo apt install -y package_name
sudo apt install -y package_name1
sudo apt install -y package_name2
Enter fullscreen mode Exit fullscreen mode

As function

function __application_log(){
    if [ "${__verbose_mode}" == "no"  ]; then
        exec 1> >(tee -a /tmp/app.log >/dev/null)
    else
        exec 2> >(tee -a /tmp/app.log >&2) > >(tee -a /tmp/app.log")
    fi
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)