简体   繁体   English

检测操作系统类型并设置JAVA_HOME

[英]Detect os type and set JAVA_HOME

I would like to detect the os type in a bash script and set JAVA_HOME accordingly. 我想在bash脚本中检测os类型,并相应地设置JAVA_HOME。

if   [[ $(type -t apt-get) == "file" ]]; then os="apt"
    elif [[ $(type -t yum)     == "file" ]]; then os="yum"
    else
            echo "Could not determine os."
    fi

case "$os" in

        apt)    pushd /etc/ \
                echo 'export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/' >> /etc/profile ;;

        yum)    pushd /etc/profile.d/ \
                echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/' >> /etc/profile.d/user_env.sh ;;
esac

I have tried this but it seems to not be writing the export to the files. 我已经尝试过了,但是似乎没有将导出写入文件中。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

I'm not sure what purpose the pushd serves here, but you don't want the \\ because that would be a continuation of the pushd command line and not actually run the echo command. 我不确定pushd用途是什么,但是您不希望使用\\因为这将是pushd命令行的延续,而不是实际运行echo命令。 You want, I think: 您想,我认为:

if   [[ $(type -t apt-get) == "file" ]]; then os="apt"
elif [[ $(type -t yum)     == "file" ]]; then os="yum"
else
    echo "Could not determine os."
fi

case "$os" in
        apt)    echo 'export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/' >> /etc/profile ;;
        yum)    echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/' >> /etc/profile.d/user_env.sh ;;
esac

If you want to keep the pushd , it would be: 如果你想保持pushd ,这将是:

case "$os" in

        apt)    pushd /etc/
                echo 'export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/' >> /etc/profile ;;

        yum)    pushd /etc/profile.d/
                echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/' >> /etc/profile.d/user_env.sh ;;
esac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM