2

I'm attempting to automate resetting a Minecraft world, and copying some custom functions back in... The reset just means deleting the world folder(s). Copying the custom functions to the right spot should work whether the world has been recreated yet or not.

The whole script is below, but the parts that are failing are these lines:

mkdir "-p $world/datapacks"
cp "-R /srv/datapacks_dev/rolla $world/datapacks"

Both of these lines trigger a similar message:

mkdir: invalid option -- ' '
Try 'mkdir --help' for more information.

Yet when I echo the quoted parts it looks right.

#!/bin/bash
if [ -d "/srv/$1/world" ]
then
  read -r -p "Are you sure? [y/N] " response
  if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
  then
    world="/srv/$1/world"
    world_nether="/srv/$1/world_nether"
    world_the_end="/srv/$1/world_the_end"
    docker container stop $1
    rm -r $world
    rm -r $world_nether
    rm -r $world_the_end
    mkdir "-p $world/datapacks"
    cp "-R /srv/datapacks_dev/rolla $world/datapacks"
    docker container start $1
    exit 0
  else
    exit 1
  fi
else
  echo "Copy failed. /srv/$1/world doesn't exist."
  exit 1
fi

I've read dozens of posts thinking this must be common, but have not found an answer... How does one do this?

1 Answer 1

5

Your shell interprets your quoted argument as one single string, not as an option and an argument. Don't quote options and quote each argument separately

Will fail:

mkdir "-p $world/datapacks"
cp "-R /srv/datapacks_dev/rolla $world/datapacks"

Won't fail:

mkdir -p "$world/datapacks"
cp -R "/srv/datapacks_dev/rolla" "$world/datapacks"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.