问题描述
我正在尝试制作一个脚本,该脚本可自动下载SpigotMC BuildTools,检测输入版本(sh build.sh)并设置一个变量,如果该版本大于或等于1.14(因为此版本,SpigotMC已停产)默认情况下,craftbukkit的自动编译自动化。现在,它仅编译子接口jar,我出于个人目的需要对它们进行编译。)
这是我尝试过的(我是linux新手,所以可能会搞砸了):
#!/bin/bash
# Sources:
# Append variables in bash : https://www.cyberciti.biz/faq/howto-linux-unix-bash-append-textto-variables/
# Check MANIFEST.MF content from jar file : https://www.manongdao.com/q-106728.html
# Check for specified property json file : https://stackoverflow.com/questions/34543829/jq-cannot-index-array-with-string
# https://www.ultralinux.org/post/json-bash/
# Bash Array : https://unix.stackexchange.com/questions/253892/syntax-error-unexpected-when-creating-an-array
# #######################################################################################################################################
#
# #######################################################################################################################################
# BuildTools Jar Info Variables
# -----------------------------
# BuilsTools working directory
buildtools_root="$(pwd)" # We use $(command) to store "command" output in a variable. We use ${command} if we have to stick other strings near it without whitespace.
# BuildTools jar location
buildtools_jar="$buildtools_root/BuildTools.jar"
# Current BuildTools jar version
buildtools_ver="$(unzip -p "$buildtools_jar" META-INF/MANIFEST.MF | grep 'Implementation-Version:' | cut -d '-' -f 5)"
# Jenkins Api-related variables.
# A. Retrieve BuildTools lastSuccessfulBuild link
targetJar="$(curl -s 'https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/api/json' | jq -r '.artifacts[].relativePath')"
lastSuccessfulBuild="$(curl -s 'https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/api/json' | jq -r '.url')"
# B. Make the download link
latestBuildToolsUrl="${lastSuccessfulBuild}artifacts/$targetJar"
# Latest BuildTools Build Version
latestBuildToolsVersion="$(curl -s 'https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/api/json' | jq -r '.number')"
displayFullName="$(curl -s 'https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/api/json' | jq -r '.fullDisplayName')"
# ---------------------------------------------------------------------------------------------------------------------------------------
# BuildTools Jar Run Variables
# -----------------------------
#
# #######################################################################################################################################
# This is a function to set java home to current java version in use.
javahome_set () {
JAVA_HOME=$(dirname "$(dirname "$( readlink -f /etc/alternatives/java )")")
OIFS=$IFS
IFS=':';
for i in $VAR;
do
JAVA1=$i/bin/java
JAVA2=$i/java
if [ -d "$i" ];
then
if [ ! -L "$JAVA1" ] && [ -x "$JAVA1" ] || [ ! -L "$JAVA2" ] && [ -x "$JAVA2" ]; then
echo "dropping path: $i";
else
NEW=$NEW:$i
fi
fi
done
IFS=$OIFS
JAVA_HOME=$NEW:$JAVA_HOME/bin
JAVA_HOME=${JAVA_HOME#:*}
}
javahome_set
# This function requires arguments. Checks if $1 >= $2
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
# This is a function to download the latest BuildTools version and check if download is successful.
buildtools_download () {
curl --silent "$latestBuildToolsUrl" --output BuildTools.jar #Request download to BuildTools.
if [ "$?" -eq 0 ]; then #Check if file was downloaded. It returns error code non-zero if file was not properly downloaded. (in this language)
echo "$Green Successful downloaded BuildTools.$Color_Off"
else
echo "$Red Error while downloading BuildTools. $Color_Off"
fi
}
# This is a function to check for updates for BuildTools jar
buildtools_check () {
if [ ! -e "$buildtools_jar" ]; then #If BuildTools.jar does NOT exist in "BuildTools" folder
echo "$Yellow Downloading BuildTools..."
buildtools_download
exit
elif [ -e "$buildtools_jar" ]; then
if [ "$buildtools_ver" -lt "$latestBuildToolsVersion" ]; then
echo "$Blue Updating BuildTools... $Color_Off"
rm "$buildtools_jar"
buildtools_download
fi
else
echo "$Cyan BuildTools is up to date. $Color_Off"
fi
}
info_menu () {
echo "==============================================[Local-Info]=============================================="
echo "BuildTools Root : $buildtools_root"
echo "Executable Path : $buildtools_jar"
echo "Installed Version : $buildtools_ver"
echo "JAVA_HOME Directory : $JAVA_HOME"
echo "=============================================[JsonAPI-Info]============================================="
echo "Latest Download Url: $latestBuildToolsUrl"
echo "========================================================================================================"
echo
if [ "$buildtools_ver" -eq "$latestBuildToolsVersion" ]; then
echo "$Green You have the latest SpigotMC BuildTools version. $Color_Off"
else
echo "$Yellow A new build is avalabele : $displayFullName"
fi
echo
}
if [ -z "$1" ]; then
echo "$BCyan Usage: $0 $BBlue<version> $Color_Off" & exit #By default,$0 is the name of this file
else
if [ -d "$1" ]; then #If argument is defined
if [ "$1" = "latest" ]; then
buildtools_check
else
vercomp "$1" "1.14"
if [ $? -eq 0 -o $? -eq 1 ]; then
BothJars=(--compile craftbukkit,spigot) # means $1 >= 1.14 ; sets the $BothJars variable
buildtools_check
fi
if [ "$1" -lt "1.14" ]; then
echo "$Yellow You want to build an older server version. Good choice btw."
buildtools_check
fi
fi
fi
rm -rf "$1" && mkdir "$1"
cd "$1" || exit
java -jar ./BuildTools.jar --rev "$1" "${BothJars[@]}" --generate-source --generate-docs #../filename is for executing it from one dir far.
我已经发表评论供您了解发生了什么事情。
有人可以帮助我解决这个问题吗?
编辑:如果您曾经玩过《我的世界》,或者看到的《我的世界》有不同的版本,则可能会遇到以下版本格式:
1.7.10; 1.8; 1.13.2; 1.14; 1.15.3(并且没有,没有“ 1.8.0”版本。只有“ 1.8”)
以下是在tr
命令之后版本为数学假的版本之间的比较示例:
1.8 > 1.7.10
-18> 1710 1.14 > 1.13.2
-114> 1132
1.15.2 < 1.16
-1152
所以...可能存在一些问题,可能会影响这些数字的比较,因为它们是版本,而不是分数或整数。
编辑2:每篇文章我将不进行3次以上的编辑。谢谢Richard K.帮助我进行版本检查功能!我已经重写了脚本以使其更加完整。我将重要的部分存储在函数中,以节省空间并更显眼。 摘要:我不明白我在这里做错了什么。 U_U
编辑3:截至Rachid K的回答,我已经对此部分进行了测试:
#!/bin/bash
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
if [ -z "$1" ]; then
echo "$BCyan Usage: $0 $BBlue<version> $Color_Off" & exit #By default,$0 is the name of this file
else
if [ -d "$1" ]; then #If argument is defined
if [ "$1" = "latest" ]; then
echo "Building latest version"
else
vercomp "$1" "1.14"
rc=$?
case $rc in
0)
echo " You want to build version 1.14"
;;
1)
echo " You want to build version above 1.14"
;;
2)
echo " You want to build version below 1.14"
;;
latest)
echo " You want to build latest version."
;;
esac
fi
fi
fi
因此vercomp()
应该查看是否:$ 1等于字符串last; $ 1等于或大于1.14; $ 1低于1.14。
该功能看起来不错,但未显示echo命令。我注意到在某些情况下,由于缺少双引号,echo命令不会显示。太...把它放在它。但这不起作用。为什么?
解决方法
显然,您获得的版本是1.14之类的数字或“最新”字符串。您需要将检查分为两个测试。首先,您检查“最新”值:
if [ "$1" = "latest" ]
,如果为假,则使用此post
中提出的解决方案来检查版本vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
所以,要比较版本,只需:
vercomp "$1" "1.14"
rc=$?
case $rc in
0) echo '=';;
1) echo '>';;
2) echo '<';;
esac
,
-
我看不到在任何地方定义了
$BCyan
和$BMagenta
变量,因此将只有空白文本而不是空白文本。 -
第7行条件的问题是,您正在比较两个字符串值,但实际上您想比较实数(或字符串“最新”)。对于实数,最好使用
bc
实用程序。这是与您要执行的操作等效的代码(我认为):
if [ `echo "$1>=1.14"|bc` -eq 1 ] || [ $1 == "latest" ]; then