【问题标题】:Detect os type and set JAVA_HOME检测操作系统类型并设置 JAVA_HOME
【发布时间】:2014-09-24 17:05:32
【问题描述】:

我想在 bash 脚本中检测操作系统类型并相应地设置 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

我已经尝试过了,但它似乎没有将导出写入文件。

非常感谢任何帮助。

【问题讨论】:

  • $os 的实际设置是什么?
  • 只是一点点:您检查的是包管理器类型,而不是操作系统类型。
  • @chepner 好的,谢谢。

标签: bash shell ubuntu-14.04 java-home


【解决方案1】:

我不确定pushd 在这里的用途,但您不想要\,因为那将是pushd 命令行的延续,而不是实际运行 echo 命令。你想要,我想:

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

如果你想保留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

【讨论】:

  • 谢谢你,你是对的,pushd 在那里没有用处。
猜你喜欢
  • 2011-03-13
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-03
  • 2012-02-05
  • 2014-06-25
相关资源
最近更新 更多