• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kristopolous/TickTick: JSON in your Bash scripts

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

kristopolous/TickTick

开源软件地址:

https://github.com/kristopolous/TickTick

开源编程语言:

Shell 98.2%

开源软件介绍:

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lukas-krecan/json2xml: Java JSON to XML converter发布时间:2022-07-09
下一篇:
onryldz/x-superobject: Delphi Cross Platform Rapid JSON发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap