运行 shell 脚本而不显示所有日志

运行 shell 脚本而不显示所有日志

是否可以以“静默”模式运行此脚本,以便仅echo显示 ' ?所有其他输出应存储在日志文件中。

#!/bin/sh

which docker && docker --version | grep "Docker version"
if [ $? -eq 0 ]
then
    echo "already existing"
else
    sudo apt-get update && sudo apt-get upgrade -y
    apt-cache policy docker-engine

    sudo apt-get install -y \
        linux-image-extra-$(uname -r) \
        linux-image-extra-virtual \
        docker-engine

    sudo service docker start

    which docker && docker --version | grep "Docker version"

    if [ $? -eq 0 ]
    then
        echo "installed correctly"
    else
        echo "installation failed" >&2
    fi
fi

答案1

是的,如果你改变一些行就可以:

#!/bin/sh

which docker && docker --version | grep -q "Docker version"
if [ $? -eq 0 ]
then
    echo "already existing"
else
    sudo apt-get update && sudo apt-get upgrade -y 2>&1 >something.log
    apt-cache policy docker-engine 2>&1 >something.log

    sudo apt-get install -y \
        linux-image-extra-$(uname -r) \
        linux-image-extra-virtual \
        docker-engine 2>&1 >something.log

    sudo service docker start 2>&1 >something.log

    which docker && docker --version | grep -q "Docker version"

    if [ $? -eq 0 ]
    then
        echo "installed correctly"
    else
        echo "installation failed" >&2
    fi
fi

相关内容