我希望检测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我试过了,但似乎没有将导出写到文件中。
任何帮助都是非常感谢的。
发布于 2014-09-24 17:26:28
我不知道pushd在这里有什么用途,但是您不需要\,因为这将是pushd命令行的延续,而不是实际运行回显命令。你想要的话我想:
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 ;;
esachttps://stackoverflow.com/questions/26022620
复制相似问题