#!/bin/bash
for i in instances/*; do
#echo $i
if [[ -d "${i}/.minecraft/" ]]; then
echo "${i}/.minecraft/"
if [ -h "${i}/.minecraft/saves" ]; then
rm -f "${i}/.minecraft/saves"
else rm -rf "${i}/.minecraft/saves";
fi
ln -s saves '"'${i}/.minecraft/saves'"'
fi
done
this is my script for linking up the saves in my PolyMC setup. the problem is that the code doesnt work if DIRECTORY has a space:
instances/1.19 Sodium/.minecraft/ this has the following output:
ln: target 'Sodium/.minecraft/saves"': No such file or directory
also removing '"' still has the same error
As everyone already commented, it's not about removing the
'"'but to quote your$iinside double quotes. Nor is it a random combination of " and ' to make it work.Explanations :
When
icontains a space like :i="1.19 Sodium"(here the double quotes are delimiters, not characters of the string),the line
ln -s saves '"'${i}/.minecraft/saves'"'is seen as :
ln -s saves '"'1.19 Sodium/.minecraft/saves'"'In this context, single quotes are string delimiters, while double quotes are any (unwanted) characters in the string.
It is read as :
ln,-s,saves,"concatenated to the word1.19,i) interpreted (since outside quotes) as a field separator,Sodium/.minecraft/savesconcatenated to a string (delimited by single quotes) of one character".Then it's exactly equivalent to :
ln -s saves '"1.19' 'Sodium/.minecraft/saves"'Which is not what you want, and why the error message says
Sodium/.minecraft/saves"does not exist.As @tkausl commented, the solution is
"${i}/.minecraft/saves"(or without the braces"$i/.minecraft/saves").With this, when
icontains a space like :i="1.19 Sodium",the line
ln -s saves "$i/.minecraft/saves"is seen as :
ln -s saves '1.19 Sodium/.minecraft/saves',ln -s saves "1.19 Sodium/.minecraft/saves",ln -s saves 1.19\ Sodium/.minecraft/saves.With the correction, any space in
iwill no longer be interpreted as a field separator becauseiis in a string delimited by double quotes.Conclusion: Never use non-quoted variables (even
echo "$i"instead ofecho $i).