So, I play Minecraft on various Linux devices, and to sync progress between devices, I used a 32GB USB 3.0 Flash Drive (Overkill, I know) equipped with a folder containing the data from a .minecraft folder, and a Minecraft.jar file to launch the entire combo. Now, portable minecraft isn't that possible on linux, so I implemented an additional shell script file to copy the appdata to the user's (the one who runs the file) home folder and use sudo priviledges to rename the folder to .minecraft since cp omits hidden files.

Now, the question is, to copy the folder over from my pendrive, I need to navigate to the drive in question. But wait! For those of you who don't know, to navigate to a drive from cmd, you need to know the user's name. Now, I just want the whole experience to be seamless (by that I mean not opening the shell script every time and editing the user), so I need to know a way I could substitute the /media/user/MC path for a variable which automagically navigates to the drive in question. Oh, and while you're at it, I'd also like to find a way to copy the updated game files (after let us say building something and saving it) back to the flash drive.

Here's a sample of the code I was using to run Minecraft on plugging in the flash drive.

sudo rm -rf ~/.minecraft 
sudo cp -rn  /media/pi0/MC/minecraft  ~/
sudo mv /home/pi0/minecraft /home/pi0/.minecraft
java -jar Minecraft.jar

If all of the systems you are using utilize BASH (as suggested by your hashbang statement in your script), you could edit your third line to use EUID environment variable:

sudo cp -rn /media/${EUID}/MC/minecraft ~/

$EUID will return your Effective User ID. It works with sudo, which means it knows the difference between a sudo escalated command and a non-escalated command.

The following simply demonstrates how it responds when called by me, by me with sudo, and by root

[sheepd0g@sys42 ~]$ echo $EUID
[sheepd0g@sys42 ~]$ sudo echo $EUID
[sheepd0g@sys42 ~]$ su -c 'echo $EUID'
  • Thanks for your help 0xSheepdog. I really appreciated it and it worked very well. By the way, I always use bash systems since I use debian-based systems. – Matt Muscat May 23 '17 at 18:58
  • Glad to help. Would you please mark one of the answers that helped as "accepted" so future visitors to the site have a complete question-answer to review? – 0xSheepdog May 23 '17 at 19:45

I don't know, if it helps you, but you can rewrite you code as here:

#!/bin/bash -l
#$ -S /bin/bash
#$ -N $1

rm -rf ~/.minecraft
cp -rn  /media/$1/MC/minecraft  ~/
mv /home/$1/minecraft /home/$1/.minecraft
java -jar Minecraft.jar

and you can specify user in argument: sh this_script.sh user will use path /media/user.

If you need current user, use:

sh this_script.sh `whoami`

Maybe you can add sudo before commands, if you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.