I want $MCVERSION to equal the "latest/release" value above (in this case, MCVERSION=1.7.2). I'm perfectly OK with pulling the JSON file down locally first via "wget" command. In fact, I'd prefer that so I can reference it if needed if my script fails (in case they change their JSON file format).
Here is my script (not that it matters too much for this example, I doubt).
#!/bin/bash
#### Define the current version of Minecraft
MCVERSION=1.7.2
#### Remove any current versions and create new folder for the JAR file
cd /home/mcmaps/.minecraft/versions
rm * -rf
mkdir $MCVERSION
cd /home/mcmaps/.minecraft/versions/$MCVERSION/
#### Download fresh copy of Minecraft JAR file for use of textures
wget https://s3.amazonaws.com/Minecraft.Download/versions/$MCVERSION/$MCVERSION.jar -P ~/.minecraft/versions/$MCVERSION/
Currently, the script results in creating this file:
/home/mcmaps/.minecraft/versions/1.7.2/1.7.2.jar
The goal is to do that without ever having to manually modify the MCVERSION variable in this script.
To do so generically might be beyond even bash variables. But you could always smash the paths, just might have to have counters for multiple contructs at a level... so you might have
Might not be horrible writing something in awk or sed to produce this type of output so it can be sourced into a program. Due to the counters, awk is the better candidate.
In your case, it could be very simple if you are just needing the one thing though. Just pull the line out, and do a simple translate.
(I guess I'm trying to push you toward a solution without handing one to you)
This is a hack but will give you the latest version from the URL you posted.
The command "curl" IS required otherwise the default builtins are used...
Note this is on a Macbook Pro 13 inch, circa August 2012, OSX 10.7.5, Default bash terminal.
I have left the commented out commands so that you can re-edit them in again to see
how it works...
#!/bin/bash --posix
# OSX 10.7.5, default bash terminal.
# The _json_ version can be virtually anything but must NOT include '"' and ','...
# Obtain the relevant _text_ file.
# NOTE THAT THIS IS A HACK!
curl "https://s3.amazonaws.com/Minecraft.Download/versions/versions.json" > /tmp/jsonfile.dat
# Get the file into a variable...
text=$(cat < /tmp/jsonfile.dat)
# echo "$text"
# Ensure IFS is stored for future rplacement.
ifs_str="$IFS"
IFS=" "
# Use an array as a hack to get to the correct part...
array=($text)
# Check that the file contains the MCVERSION number with extras.
# This assumes that the file format remains the same upon every version.
version=$(echo "${array[5]}")
# echo "$version"
# You now have the version wrapped in inverted commas and a comma too...
# From here you can remove the '"' and ',' using builtins...
MCVERSION=""
decimal=0
subscript=0
length=$[ ${#version} - 1 ]
while [ $subscript -le $length ]
do
# Remove the inverted commas...
decimal=$(printf "%d" \'${version:$subscript:1})
if [ $decimal -eq 44 ]
then
break
fi
if [ $decimal -eq 34 ]
then
subscript=$[ $subscript + 1 ]
else
MCVERSION=$MCVERSION${version:$subscript:1}
subscript=$[ $subscript + 1 ]
fi
done
# Print the final string...
echo ""
echo "$MCVERSION"
echo ""
# Replace the original IFS...
IFS="$ifs_str"
# Get the length as a final check...
# length="${#MCVERSION}"
# echo "$length"
The results on this machine...
Last login: Fri Nov 1 19:03:30 on ttys000
AMIGA:barrywalker~> ./json.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15240 100 15240 0 0 13546 0 0:00:01 0:00:01 --:--:-- 15179
1.7.2
AMIGA:barrywalker~> _