Bash - PaperMC 安装脚本

Bash - PaperMC install script

我试图制作一个脚本来自动设置 Minecraft Java 版本服务器(PaperMC 平台)

脚本应该可以通过PaperMC的JSON API获取最新的服务器版本。 此外,服务器的安装过程应该是简单和自动的,用户的手动干预最少甚至没有。

我也想优化一下服务器,不要太耗资源。

以下是我制作此脚本的尝试:

#!/bin/bash

# Script nane : paper-install.sh

latestVer="$(curl -s 'https://papermc.io/api/v1/paper' | jq -r '.versions[0]')"

if [ ! -e paper-*.jar ]; then
    echo "Downloading PaperMC version $latestVer ..."
    rm -rf paper-*.jar && curl -OLJ https://papermc.io/api/v1/paper/"$latestVer"/latest/download
    timeout 3s echo a
    else
    echo Downloaded
fi


start_script () {

cat <<'EOF' > run_paper
#!/bin/bash

# Find paper-*.jar file
jarfile=$(find . -name paper-*.jar)

# Server run arguments
java -jar $jarfile -Xmx1G -Xms1G -d64 -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+UseNUMA -XX:+CMSParallelRemarkEnabled -XX:MaxTenuringThreshold=15 -XX:MaxGCPauseMillis=30 -XX:GCPauseIntervalMillis=150 -XX:+UseAdaptiveGCBoundary -XX:-UseGCOverheadLimit -XX:+UseBiasedLocking -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=15 -Dfml.ignorePatchDiscrepancies=true -Dfml.ignoreInvalidMinecraftCertificates=true -XX:+UseFastAccessorMethods -XX:+UseCompressedOops -XX:+OptimizeStringConcat -XX:+AggressiveOpts -XX:ReservedCodeCacheSize=2048m -XX:+UseCodeCacheFlushing -XX:SoftRefLRUPolicyMSPerMB=2000 -XX:ParallelGCThreads=10 nogui
EOF

# Make the script executable
chmod +x run_paper

}

# Generate script
if [ ! -e run_paper ]; then
    echo "Generating run script ..."
    start_script
    else
    echo .
fi

# Run script
bash run_paper 

# after jar generated eula.txt , run this command
# sed -i 's/false/true/i' eula.txt

当我运行这个脚本时,我遇到了很多我不知道如何解决的错误debug/describe,这确实很糟糕。

我想知道我做错了什么...

ShellCheck 网站帮助了我,但在这段代码上 - 没那么多...

尝试修复您的脚本

使用 shellcheck linter 的建议仍然有效。

#!/usr/bin/env bash

# Script nane : paper-install

API_URL='https://papermc.io/api/v1'
IFS=$(printf '\n\r\t ')

latestVer=$(curl -s "$API_URL/paper" | jq -r '.versions[0]')
location="$API_URL/paper/$latestVer/latest/download"
latestJar=$(
  curl \
    --silent \
    --head \
    --location "$location" |
    awk -F= '/content-disposition: attachment; filename=/{print}'
)
latestJar=${latestJar%%[$IFS]}

if [ ! -e "$latestJar" ]; then
  
  printf 'Downloading PaperMC version %s from %s as %s ...\n' \
    "$latestVer" "$location" "$latestJar"
  curl \
    --output "$latestJar" \
    --location "$location"
  ln -sf "$latestJar" paper.jar
  timeout 3s echo a
else
  echo Downloaded
fi

get_java8() {
  update-alternatives --query java | awk '/Alternative:/{print}' |
    while read -r jvm; do
      if "$jvm" -version 2>&1 | grep -q 'version "1.8.*"'; then
        echo "$jvm"
        break
      fi
    done
  # Found no java 8
}

generate_start_script() {
  cat <<EOF >run_paper
#!/usr/bin/env bash

# The paper jar
jarfile=paper.jar

# The java command path
#java_cmd=$(command -v java)
java_cmd=$(env printf %q "")

# Long list of unknown wizard's black magic JVM arguments.
# I have no idea if these help, are really needed,
# or how many will break with newer java versions
java_args=(
  -Xmx1G -Xms1G -d64 -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC
  -XX:+UseParNewGC -XX:+UseNUMA -XX:+CMSParallelRemarkEnabled
  -XX:MaxTenuringThreshold=15 -XX:MaxGCPauseMillis=30
  -XX:GCPauseIntervalMillis=150 -XX:+UseAdaptiveGCBoundary
  -XX:-UseGCOverheadLimit -XX:+UseBiasedLocking -XX:SurvivorRatio=8
  -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=15
  -Dfml.ignorePatchDiscrepancies=true
  -Dfml.ignoreInvalidMinecraftCertificates=true -XX:+UseFastAccessorMethods
  -XX:+UseCompressedOops -XX:+OptimizeStringConcat -XX:+AggressiveOpts
  -XX:ReservedCodeCacheSize=2048m -XX:+UseCodeCacheFlushing
  -XX:SoftRefLRUPolicyMSPerMB=2000 -XX:ParallelGCThreads=10
)

# Server run arguments
"$java_cmd" "${java_args[@]}" -jar "$jarfile" nogui
EOF

  # Make the script executable
  chmod +x run_paper

  if [ ! -e eula.txt ]; then
    # Run the server to generate eula.txt
    echo 'Running server installation'
    ./run_paper
  fi
}

# Generate script
if [ ! -e run_paper ]; then
  # Get java8 path or fail
  java8_path=$(get_java8)
  if [ -n "$java8_path" ]; then
    echo "Generating run script ..."
    generate_start_script "$java8_path"
  else
    echo 'No java8 found on this system!' >&2
  fi
else
  echo .
fi