通过shell脚本获取javahome

#!/bin/bash

JAVA_BIN=`which java`

echo ${JAVA_BIN}

 

JAVA_HOME=`realpath ${JAVA_BIN}`

echo ${JAVA_HOME}

 

BASE_NAME=`basename ${JAVA_HOME}`

echo ${BASE_NAME}

 

while [[ ${BASE_NAME} == "bin" || ${BASE_NAME} == "jre" || ${BASE_NAME} == "java" ]]

do

    JAVA_HOME=`dirname  ${JAVA_HOME}`

     echo "JAVA_HOME: "  ${JAVA_HOME}

    BASE_NAME=`basename ${JAVA_HOME}`

    echo "BASE_NAME: "  ${BASE_NAME}

done

echo ${JAVA_HOME}

你可能感兴趣的:(Linux,javahome,shell脚本)