我是批处理脚本编码的新手,有点卡在这里。我有一个.sh文件,检查安装的java版本是否至少为1.8.0:
# Minimal version
MINIMAL_VERSION=1.8.0
# Check if Java is present and the minimal version requirement
_java=`type java | awk '{ print $ NF }'`
CURRENT_VERSION=`"$_java" -version 2>&1 | awk -F'"' '/version/ {print $2}'` #says 1.8.0_65
minimal_version=`echo $MINIMAL_VERSION | awk -F'.' '{ print $2 }'` #says 8
current_version=`echo $CURRENT_VERSION | awk -F'.' '{ print $2 }'` #says 8
if [ $current_version ]; then
if [ $current_version -lt $minimal_version ]; then
echo "Error: Java version is too low. At least Java >= ${MINIMAL_VERSION} needed.";
exit 1;
fi
else
echo "Not able to find Java executable or version. Please check your Java installation.";
exit 1;
fi
很清楚。我需要为Windows编写一个完全相同逻辑的.bat文件。所以这就是我被困的地方,因为我不知道awk的windows类似物。
答案 0 :(得分:3)
Here you can see how you can get java version就像整数,可以进行数字比较:
@echo off
:: uncomment the line bellow if the java.exe is not in the %PATH%
::PATH %PATH%;%JAVA_HOME%\bin\
java -version 1>nul 2>nul || (
echo no java installed
exit /b 2
)
for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do set "jver=%%j%%k%%l%%m"
if %jver% LSS 18000 (
echo java version is too low
echo at least 1.8 is needed
exit /b 1
)