我有以下脚本:
#!/bin/bash
# usage: [path_to_script]/minecraft.bash [functions_to_run]
# example: /srv/minecraft/minecraft.bash stop "backup 10"
# -------------------------------- settings --------------------------------
# paths
BACKUP_DIRECTORY="backup"
SERVER_NAME="paper-296.jar" # the filename of your server jar
WORLD_NAME="matigcraft" # level-name in server.properties
# messages
BACKUP_KICK_MESSAGE="The server is making a backup, and will be back online in a few minutes"
BACKUP_WARNING_MESSAGE="The server will go offline to make a backup in X minute(s)"
STOP_KICK_MESSAGE="The server is shutting down for maintenance"
STOP_WARNING_MESSAGE="The server will go offline for maintenance in X minute(s)"
# amount of backups to keep when cleaning
BACKUP_AMOUNT=28
# JVM arguments to use when starting the server
JVM_ARGUMENTS="-Xms6G -Xmx6G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true"
# -------------------------------- functions --------------------------------
# check if the server is running
function status {
screen -ls | grep -q -w "$WORLD_NAME" \
&& echo $WORLD_NAME is running && return 0 \
|| echo $WORLD_NAME is not running && return 1
}
# if running, stop the server and wait
# call with number to set as warning delay
# call with additional text to use as warning message, defaults to STOP_WARNING_MESSAGE
# call with additional text to use as kick message, defaults to STOP_KICK_MESSAGE
function stop {
status > /dev/null && {
[ -z "$1" ] || {
screen -S "$WORLD_NAME" -X stuff "say ${{2:-$STOP_WARNING_MESSAGE}//X/$1}\n"
sleep $(($1 * 60))
}
screen -S "$WORLD_NAME" -X stuff "kick @a ${3:-$STOP_KICK_MESSAGE}\n""stop\n"
while status > /dev/null; do sleep 1; done
}
}
# make a backup of the world
# call with number to set as warning delay
# call with additional text to use as warning message, defaults to BACKUP_WARNING_MESSAGE
# call with additional text to use as kick message, defaults to BACKUP_KICK_MESSAGE
function backup {
stop "$1" "${2:-$BACKUP_WARNING_MESSAGE}" "${3:-$BACKUP_KICK_MESSAGE}"
mkdir -p "$BACKUP_DIRECTORY"
zip -9 -r "$BACKUP_DIRECTORY"/"$WORLD_NAME"_"$(date +%Y_%m_%d_%H%M)".zip "$WORLD_NAME"
}
当我调用时"say ${{2:-$STOP_WARNING_MESSAGE}//X/$1}\n"
,会在stop
函数中出现“错误替换”错误backup 10
。ShellCheck认为我的语法很好,并且可以确定我正在Bash中运行脚本。当我执行T="${2:-$STOP_WARNING_MESSAGE}"
和时${T//X/$1}\n"
,一切都会按预期进行。
我认为此解决方案以前没有任何问题,但我可能是错的。我的语法有问题吗?或者更新后Bash出现问题?
{2:-$STOP_WARNING_MESSAGE}
是提供默认值的分配。它不是变量。Bash参数扩展需要以下形式:
${var//substring/replacement}
由于括号括起来的表达式不是var
,因此是不好的替代。你可以使用一个临时变量:
myvar=${2:-$STOP_WARNING_MESSAGE}
然后
${myvar//X/$1}
我猜我在那之前是错的。没有临时变量,有没有办法做到这一点?
否。在某些情况下,
${...}
解析为数字,例如允许嵌套的长度,但是在var
需要a的情况下,这需要一个临时时间。