命令仅在用户运行它时才有效,而不是通过 eval 运行

命令仅在用户运行它时才有效,而不是通过 eval 运行

我正在尝试自动构建。我制作了一个完整的脚本,可以查看这里,但我举了一个最小的例子:

#!/bin/bash

trap "exit" INT

DIR="spigot/spigotJars/install"
if [ -d "$DIR" ]; then
   echo "$DIR already created" > /dev/null
else
   mkdir -p $DIR
   chmod -R +x $DIR
fi
cd $DIR || exit

extension=.tar.gz
executable=

export IFS=";"
java8=unknown

function installJDK() {
   echo "Failed to find Java $1. Downloading it..."
   curl -L -o "Java$1$extension" "$2"
   mkdir "jdk-$1"
   if [[ $extension == ".zip" ]]; then
      unzip "Java$1$extension" -d "jdk-$1"
   else
      tar -xvf "Java$1$extension" -C "jdk-$1" --strip-components 1
   fi
   rm "Java$1$extension"
}

if [[ $java8 == *"unknown"* ]]; then
    if [ -d "./jdk-8" ]; then
        echo "Java 8 and already downloaded, using this version..."
    else
        installJDK 8 "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u345-b01/OpenJDK8U-jdk_x64_${os}_hotspot_8u345b01$extension"
    fi
    java8="$PWD/jdk-8/bin/java$executable"
fi


curl -o BuildTools.jar https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar

mcCmd="\"$java8\" -jar BuildTools.jar --rev 1.8.8"
echo "Running $mcCmd ..."
eval "$mcCmd"

使用此脚本时,我收到此错误:

Error: Could not find or load main class  -Xmx1024M
Exception in thread "main" java.lang.RuntimeException: Error running command, return status !=0: [sh, /home/tmp/Negativity/spigot/spigotJars/install/apache-maven-3.6.0/bin/mvn, -Dbt.name=582a, install:install-file, -Dfile=work/mapped.ba9a3cce.jar, -Dpackaging=jar, -DgroupId=org.spigotmc, -DartifactId=minecraft-server, -Dversion=1.8.8-SNAPSHOT]
        at org.spigotmc.builder.Builder.runProcess0(Builder.java:973)
        at org.spigotmc.builder.Builder.runProcess(Builder.java:904)
        at org.spigotmc.builder.Builder.runMaven(Builder.java:873)
        at org.spigotmc.builder.Builder.main(Builder.java:552)
        at org.spigotmc.builder.Bootstrap.main(Bootstrap.java:27)

但是,当我手动运行该命令时(对我来说"/home/tmp/spigot/spigotJars/install/jdk-8/bin/java" -jar BuildTools.jar --rev 1.8.8),它运行良好。

我该如何修复它?

答案1

该问题来自export IFS=";"。由于这是针对 Windows 用户的修复程序,因此我检查了它是来自 linux 还是 windows 环境。

这是我制作的代码(截断):

case "$OSTYPE" in
  solaris*) os=solaris ;;
  darwin*)  os=macos ;;
  linux*)   os=linux ;;
  bsd*)     os=bsd ;;
  msys*)    os=windows ;;
  cygwin*)  os=windows ;;
  *)        os=unknown:$OSTYPE ;;
esac


if [[ $os == "windows" ]]; then
   extension=.zip
   executable=.exe
   export IFS=";"
fi
echo "OS used: $os"

现在它对每个人都有效。

相关内容