I would like to detect the os type in a bash script and set JAVA_HOME accordingly.
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. 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:
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