kristopolous / TickTick

JSON in your Bash scripts

Home Page:http://9ol.es/TheEmperorsNewClothes.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect output

kuldeepdhaka opened this issue · comments

Got from http://9ol.es/TheEmperorsNewClothes.html

#!/bin/bash
. ticktick.sh

``
  people = {
    "Writers": [
      "Rod Serling",
      "Charles Beaumont",
      "Richard Matheson"
    ],
    "Cast": {
      "Rod Serling": { "Episodes": 156 },
      "Martin Landau": { "Episodes": 2 },
      "William Shatner": { "Episodes": 2 }
    }
  }
``

function printDirectors() {
  echo "  The ``people.Directors.length()`` Directors are:"

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

`` people.Directors = [ "John Brahm", "Douglas Heyes" ] ``
printDirectors

newDirector="Lamont Johnson"
`` people.Directors.push($newDirector) ``
printDirectors

echo "Shifted: "``people.Directors.shift()``
printDirectors

echo "Popped: "``people.Directors.pop()``
printDirectors

Generates:

  The 2 Directors are:
    - John
    - Brahm
    - Douglas
    - Heyes
  The 3 Directors are:
    - John
    - Brahm
    - Douglas
    - Heyes
    - Lamont
    - Johnson
Shifted: John Brahm
  The 2 Directors are:
    - Douglas
    - Heyes
    - Lamont
    - Johnson
Popped: Lamont Johnson
  The 1 Directors are:
    - Douglas
    - Heyes

GNU bash, version 4.4.23(1)-release

This is not really a problem with TickTick. It is a problem with the script that is invoking it, and with the TickTick README. Specifically, the for loop in the printDirectors function (which is printEmployees in the README).

In this example, which is clearly derived from the README (meaning the original error is actually there), the printf line in the for loop says:

printf " - %s\n" ${!director}

This line is missing quotes around the variable, which makes printf see it as two separate %s candidates. It should be written like this:

printf " - %s\n" "${!director}"

Interestingly, this is done correctly in the example.sh file, though not so in the README.md file. (@kristopolous might want to fix that, if this project is still being maintained).