我正在尝试制作一个脚本,自动下载SpigotMC BuildTools,检测输入版本(sh build.sh <version>
(,并设置一个变量,如果版本大于或等于1.14,则使用该变量(自本版本以来,SpigotMC默认停止了craftbukkit的自动编译。现在它只编译spiot-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 available : $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
命令后数学错误的版本之间的比较示例:
CCD_ 3-18>17101.14 > 1.13.2
-114>1132
1.15.2 < 1.16
-1152<116
所以。。。可能会有一些问题影响这些数字的比较,因为它们是版本,而不是分数或整数。
编辑2:我不会做超过3次编辑/帖子。感谢Richard K.为我提供版本检查功能!我重写了我的剧本,使它更完整。我将重要部件存储在功能中,以节省空间并使其更显眼。总结:我不明白我在这里做错了什么。uu
编辑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是否等于字符串latest$1大于或等于1.14$1低于1.14。
该功能看起来不错,但不会显示echo命令。我注意到,在某些情况下,由于缺少双引号,echo命令不会显示。所以…这么说吧。但它不起作用。为什么?
显然,您得到的版本要么是1.14这样的数字,要么是"最新的";一串你需要把你的检查分成两个测试。首先检查";最新的";值:
if [ "$1" = "latest" ]
如果它是错误的,通过使用这个后中提出的解决方案来检查版本
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