I have the following script:
#!/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"
}
that gives me the "bad substitution" error on "say ${{2:-$STOP_WARNING_MESSAGE}//X/$1}\n"
in the stop
function when I call backup 10
. ShellCheck thinks my syntax is fine, and I'm certain I'm running the script in Bash. When I do T="${2:-$STOP_WARNING_MESSAGE}"
and ${T//X/$1}\n"
, everything works as it should.
I think this solution worked before without issues, but I could be wrong. Is there something wrong with my syntax? Or perhaps there is an issue with Bash after an update?
{2:-$STOP_WARNING_MESSAGE}
is an assignment providing a default value. It is not a variable. Bash parameter expansions require the form, e.g.
${var//substring/replacement}
Since your brace-enclosed expression is not a var
, it is a bad-substitution. You can use a temporary variable:
myvar=${2:-$STOP_WARNING_MESSAGE}
and then
${myvar//X/$1}
I guess I was wrong about it working before then. Is there any way to do this without a temporary variable?
No. There are some instances where
${...}
resolves to a number, such as a length that allow them to be nested, but when avar
is required -- it takes a temporary.