Sponsored Content
Top Forums Shell Programming and Scripting How to define a variable in a BASH script by using a JSON file online? Post 302870263 by wisecracker on Friday 1st of November 2013 03:12:39 PM
Old 11-01-2013
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...
Code:
#!/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...
Code:
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~> _

It is now up to you to modify as required...

Hope it helps...
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

2. Shell Programming and Scripting

bash - define a variable

Hello, I would like to define a variable based on another variable: a=5 b$a=100 This does not work. What is the right way to do it? Thanks ---------- Post updated at 07:37 PM ---------- Previous update was at 07:33 PM ---------- Found my answer with the search function (did not... (0 Replies)
Discussion started by: jolecanard
0 Replies

3. Shell Programming and Scripting

question about define variable.

Hi, Unix Gurus, In our existing file, there is a script like #!/bin/sh step=${1:-0} cur_step=10 if ... My question is what's "${1:-0}" mean? I know it defines a variable but I don't know what's (1:-0) mean? :wall: Thanks in advance. (2 Replies)
Discussion started by: ken002
2 Replies

4. Shell Programming and Scripting

Define variable from file.

HI I have file A.txt _1A _2A _3A _4A I want define all as different variable. $1A=_1A $2B=_2A $3C=_3A $4D=_4A Now i can use any variable in my script. (3 Replies)
Discussion started by: pareshkp
3 Replies

5. Shell Programming and Scripting

Bash script - cygwin (powershell?) pull from GitHub API Parse JSON

All, Have a weird issue where i need to generate a report from GitHub monthly detailing user accounts and the last time they logged in. I'm using a windows box to do this (work issued) and would like to know if anyone has any experience scripting for GitAPI using windows / cygwin / powershell?... (9 Replies)
Discussion started by: ChocoTaco
9 Replies

6. Shell Programming and Scripting

How to define two digits variable in shell script?

Dear Shell script Experts, I am working on shell script which is defined here, qsub_seq.csh . The purpose of this script is to read few input files (with defined starting index and last index) and make processing faster over server. For some task, I had 1064 of input files, so I wrote another... (8 Replies)
Discussion started by: emily
8 Replies

7. Shell Programming and Scripting

Parsing and Editing a json file with bash script

I am trying to automate editing of a json file using bash script. The file I initially receive is { "appMap": { "URL1": { "name": "a" }, "URL2": { "name": "b" }, "URL3": { "name": "c" }, } WHat I would like to do is replace... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

8. Shell Programming and Scripting

JSON structure to table form in awk, bash

Hello guys, I want to parse a JSON file in order to get the data in a table form. My JSON file is like this: { "document":{ "page": }, { "column": } ] }, { ... (6 Replies)
Discussion started by: Gescad
6 Replies

9. Shell Programming and Scripting

Building JSON command with bash script

Hello. I'm new to bash script and I'm learning the basics by writing some scripts. Recently a friend of mine asked me if I could try to write a script to him to automate a couple of processes that uses JSON RPCs. I'll try to explain in few words the workflow just to contextualize the problem.... (48 Replies)
Discussion started by: psysc0rpi0n
48 Replies
json::write(3tcl)						       JSON							 json::write(3tcl)

__________________________________________________________________________________________________________________________________________________

NAME
json::write - JSON generation SYNOPSIS
package require Tcl 8.5 package require json::write ?1.0.1? ::json::write indented ::json::write indented flag ::json::write aligned ::json::write aligned flag ::json::write string s ::json::write array arg... ::json::write object key value... _________________________________________________________________ DESCRIPTION
The json::write package provides a simple Tcl-only library for generation of text in the JSON http://www.json.org/ data exchange format as specified in RFC 4627 http://www.ietf.org/rfc/rfc4627.txt. COMMANDS
::json::write indented This method returns the current state of the indentation setting. ::json::write indented flag This and the method aligned configure the layout of the JSON generated by the package. If this flag is set (default) the package will break the generated JSON code across lines and indent it according to its inner structure, with each key of an object on a separate line. If this flag is not set, the whole JSON object will be written on a single line, with minimum spacing between all elements. ::json::write aligned This method returns the current state of the alignment setting. ::json::write aligned flag This and the method indented configure the layout of the JSON generated by the package. If this flag is set (default) the package ensures that the values for the keys in an object are vertically aligned with each other, for a nice table effect. To make this work this also implies that indented is set as well. If this flag is not set, the output is formatted as per the value of indented, without trying to align the values for object keys. ::json::write string s This method takes the string s and returns it properly quoted for JSON as its result. ::json::write array arg... This method takes a series of JSON formatted arguments and returns them as a properly formatted JSON array as its result. ::json::write object key value... This method takes a series of key/value arguments, the values already formatted for JSON, and returns them as a properly formatted JSON object as its result, with the keys formatted as JSON strings. BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category json of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. KEYWORDS
data exchange, exchange format, javascript, json CATEGORY
CGI programming COPYRIGHT
Copyright (c) 2009-2011 Andreas Kupries <andreas_kupries@sourceforge.net> json 1.0.1 json::write(3tcl)
All times are GMT -4. The time now is 04:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy