• Stars
    star
    569
  • Rank 76,742 (Top 2 %)
  • Language
    Shell
  • License
    Other
  • Created over 12 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JSON in your Bash scripts

Introduction

TickTick enables you to put JSON in bash scripts. Yes, just encapsulate them with two back-ticks.

Note: This is just a fun hack. You may want to consider using mature languages like Ruby or Perl to solve actual real life problems. Oh who am I kidding, I use whitespace and brainfuck every day.

Usage

Proper usage (if there is such a thing), is to place the following line right after the "shbang" at the top of your script. For instance:

#!/bin/bash
#
# Nuclear_meltdown_preventer.sh
#
# This is really important stuff. Don't edit it!
#
. ticktick.sh

..

See how that's near the tippity-top? That's where it's supposed to go. If you put it lower, all bets are off. :-(

API

Arrays

A few array manipulation runtime directives are supported:

  • [] (as new Array)
    ``arr = ["foo"]``
  • [] (to index)
    echo ``arr[0]``
  • length
    arr_len=``arr.length()``; echo ${arr_len}
  • push
    ``arr.push(${arr_len})``
  • pop
    echo ``arr.pop()``
  • shift
    echo ``arr.shift()``
  • delete
    echo ``key.value.delete()``
  • items
    for x in ``arr.items()``; do echo "${x}"; done

Notes:

  • These feature do not preclude having variables by those names. You can have key.delete = 1 and then key.delete.delete()
  • Since TickTick is a Bash-emitting transpiler, things that don't work in bash (such as modifying in-shell variables in double quotes) don't work in TickTick.

Objects

  • {} (as new Object)
    ``obj = { "foo": "bar", "baz": "qux" }``
  • [] (to index)
    echo ``obj["foo"]``
  • . (to index)
    echo ``obj.baz``

Miscellaneous

tickParse - parse things inline

  • Inline parsing: You can parse a file in with the tickParse routine (see the example).

tickVars - see the currently defined variables

  • Show all variables: You can see the current values defined in the TickTick world with the tickVars routine.
  • If you don't like the output, there are some options to customize it. Try tickVars -h for more information.

tickReset - clear the currently defined variables

  • Clear all variables: You can erace any JSON you have created/imported with the tickReset routine.

__tick_var_debug - See the interim bash code

  • Dry run (display compiled code): TickTick is a mini-compiler that emits bash. If you declare export __tick_var_debug=1 at the top of your code (before you source ticktick.sh), then the code will not run but instead print what it would have run.

Bash variables ($) in JSON

Along with assignment operations1, and Javascript like indexing into objects and arrays.

Additionally, bash variables (eg., "$name") are preserved in the TickTick blocks. For instance, once could do

`` Var.Data = [] ``
`` Var.Data.push($key) ``
bashvar=`` Var.Data.pop() ``

1Although Javascript supports $ prefixed variables, this does not.

Examples

Inline Parsing

#!/bin/bash

. ticktick.sh

bob=Bob

``
  people = {
    "HR" : [
      "Alice",
      $bob,
      "Carol"
    ],
    "Sales": {
      "Gale": { "profits" : 1000 },
      "Harry": { "profits" : 500 }
    }
  }
``

function printEmployees() {
  echo
  echo "  The ``people.Engineering.length()`` Employees listed are:"

  for employee in ``people.Engineering.items()``; do
    printf "    - %s\n" "${!employee}"
  done

  echo 
}

echo Base Assignment
`` people.Engineering = [ "Darren", "Edith", "Frank" ] ``
printEmployees

newPerson=Isaac
echo Pushed a new element by variable, $newPerson onto the array
`` people.Engineering.push($newPerson) ``
printEmployees

echo Shifted the first element off: `` people.Engineering.shift() ``
printEmployees

echo Popped the last value off: `` people.Engineering.pop() ``
printEmployees

echo Indexing an array, doing variable assignments

person0=``people.HR[0]``
echo $person0 ``people.HR[1]``

Using a File or cURL

#!/bin/bash
. ../ticktick.sh

# File
DATA=`cat data.json`
# cURL
#DATA=`curl http://foobar3000.com/echo/request.json`

tickParse "$DATA"

echo ``pathname``
echo ``headers["user-agent"]``

Mailing List

Join it over here.

LICENSE

This software is available under the following licenses:

  • MIT
  • Apache 2

Parts of this work are derived from JSON.sh, which is also available under the aforementioned licenses.

More Repositories

1

BOOTSTRA.386

A vintage 1980s DOS inspired Twitter Bootstrap theme
HTML
6,512
star
2

TopLevel

A New Way to Javascript Your HTML
JavaScript
287
star
3

Porter-Stemmer

A Javascript Implementation of the Porter Stemmer
JavaScript
95
star
4

PiggyPack

Hiding Things Out In The Open
Ruby
76
star
5

mansnip

πŸ“Ί "The new way to search through manpages that everyone is talking about!"
Python
65
star
6

ascsee

An organic image viewer
C
52
star
7

db.js

Thin portable Javascript document store event-driven database
JavaScript
40
star
8

tumblr-scraper

scrapin da tumblr.
Ruby
30
star
9

Porter2-Stemmer

JavaScript
26
star
10

apophnia

Image server
C
20
star
11

_inject

Javascript Code Injection for Debugging
HTML
19
star
12

DRR

A DVR to record the radio that doesn't get podcasted
JavaScript
19
star
13

quick-color

A quick way to manipulate colors without all the nonsense in between
HTML
17
star
14

tools

General purpose tools
Shell
16
star
15

music-explorer

A music scraper, navigator, archiver, and cataloger for people looking for new sounds.
Shell
15
star
16

UniFig

Unicode Figlet conversion
CSS
12
star
17

ytmix

YouTube large playlist navigator web app.
JavaScript
9
star
18

bash-org-tools

bash.org tools
JavaScript
9
star
19

js1k

my js1k spring 2k13 submission
C
9
star
20

vimbuild

A vim builder
Vim Script
9
star
21

phpwtf

Search PHP's documentation from the command line.
PHP
8
star
22

EvDa

Events and Data
JavaScript
7
star
23

tube-get

A tube-site downloader
Python
7
star
24

skype-search

Global skype search fast and easy
JavaScript
6
star
25

aclock

ascii art clock
C
5
star
26

emptyv

80s emptyv clone
JavaScript
4
star
27

proxy

An HTTP Proxy Exclusively For Debugging Alone.
C
4
star
28

jsrand

seeded pseudo-random number generating in javascript
JavaScript
4
star
29

Coinbase-pro-portfolio-analyzer

A command-line portfolio analyzer for coinbase pro
Python
3
star
30

lists

lists of things
3
star
31

acidx

coloring of terminals
C
3
star
32

ascii-gol-helloworld

A festive ASCII art Hello world
C
3
star
33

reddit-comment-miner

Powerful Trolling Technology
3
star
34

similarcolors

Color matching system
C
3
star
35

ComputerHistoryPodcast

Shell
3
star
36

linkgo.es

url rewriter
HTML
2
star
37

super-secret-github-scraper

shh, secret secret secret
Shell
2
star
38

services-parser

parser of services file, and easy c++ lookup system
C++
2
star
39

js-copypasta

small js helpers designed to be copy pasted into code.
HTML
2
star
40

bracket

a rewritten version of db.js
JavaScript
2
star
41

reddit-user-track

just some set of tools to do some reddit stuff.
Python
2
star
42

slowchurn

a new kind of social media + commenting site
2
star
43

ocr-confirm

1
star
44

scout-mysql-nagger

Execute arbitrary mysql queries to your databank of choice.
JavaScript
1
star
45

webspace

JavaScript
1
star
46

js-shell

A javascript debugging shell
JavaScript
1
star
47

articles

the things I write.
HTML
1
star
48

githater

githater core
Python
1
star
49

frustrometer

the legendary frustrometer
HTML
1
star
50

crappy-notion

My Notion configuration. Not a good one at that.
Lua
1
star
51

news-analyzer

Manufacturing Consent meets the Internet
Python
1
star
52

curses_text

A tool for putting text in a curses window
C++
1
star
53

lowcon

Shaming unreadable websites
PHP
1
star
54

syfin

pitch deck for a fake company, weee.
HTML
1
star