Read JSON files from Bash scripts

function readJson {
  UNAMESTR=`uname`
  if [[ "$UNAMESTR" == 'Linux' ]]; then
    SED_EXTENDED='-r'
  elif [[ "$UNAMESTR" == 'Darwin' ]]; then
    SED_EXTENDED='-E'
  fi;

  VALUE=`grep -m 1 "\"${2}\"" ${1} | sed ${SED_EXTENDED} 's/^ *//;s/.*: *"//;s/",?//'`

  if [ ! "$VALUE" ]; then
    echo "Error: Cannot find \"${2}\" in ${1}" >&2;
    exit 1;
  else
    echo $VALUE ;
  fi;
}



# Site info from JSON
DOMAIN=`readJson site-info.json domain` || exit 1;
NAME=`readJson site-info.json site-name` || exit 1;
SLUG=`readJson site-info.json slug` || exit 1;
PREFIX=`readJson site-info.json prefix` || exit 1;
DESC=`readJson site-info.json description` || exit 2;Code language: Bash (bash)

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.