

Imagine a world where Bash supports JSON. - e1ven
http://qaa.ath.cx/TheEmperorsNewClothes.html?

======
pak
Wow. I was wondering how the hell TickTick.sh "extended" the bash interpreter
grammar, because there is no obvious way to do that. The key is on lines
171-189:

    
    
        awk -F '``' '{\
        
          # ... jibbery awk-ese ...
        
        }' `caller 1 | cut -d ' ' -f 3` | sed "s/^/S/g;s/$/E/g" | __tick_fun_parse | bash
    
        exit
    

So what this is doing, by Jove, is using the "caller" bash builtin + "cut" to
print out the filename of the currently executing script, pass that as an
argument to awk, and use awk + sed + the "__tick_fun_parse" function to re-
parse the script into something that can actually be executed by bash. The
"exit" command then exits the _current_ run-through of the script, because
otherwise it will reach all the stuff that would normally cause a syntax
error.

This is legit hax.

------
mhansen
Jsonpipe is excellent for this. It makes json greppable by putting every piece
if data on a new line with its key.

    
    
      $ echo '{"a": 1, "b": 2}' | jsonpipe 
      / {} 
      /a 1 
      /b 2 
      $ echo '["foo", "bar", "baz"]' | jsonpipe 
      / [] 
      /0 "foo" 
      /1 "bar" 
      /2 "baz"
    

<https://github.com/dvxhouse/jsonpipe>

~~~
a3_nm
A similar tool: <http://kmkeen.com/jshon/>

------
jayferd
I'd like to see a language that's as easy to use on the shell as bash
(optional quoting, chaining, etc), but is as powerful as the more general-
purpose scripting languages (Ruby/Python/TCL/Perl/etc). If no such thing
exists I think I'm gonna try designing one.

~~~
groovy2shoes
There's always Scheme Shell: <http://www.scsh.net/>

~~~
philjackson
Non-interactive and unmaintained, unfortunately.

------
jrockway
_You are traveling through another dimension, a dimension not only of sight
and sound but of mind. A journey into a wondrous land of imagination._

Prepare to enter... the scary door.

~~~
troels
Indeed :) In which case, you'd love this: [http://www.debian-
administration.org/article/A_web_server_in...](http://www.debian-
administration.org/article/A_web_server_in_a_shell_script)

~~~
jacquesm
Be sure to allow prefixes of ../.. for interesting files.

------
aidenn0
This violates my number one rule of shell scripting:

Don't use symmetrical quotes

~~~
harbud
So you never use 'single' and "double" quotes?

------
mattbillenstein
Number one thing I learned from this -- don't write code in bash ...

~~~
e1ven
Oh, Man, I can't agree. I LOVE using bash for quick scripting, because I can
use the same commands I use when I run them in the shell.

Doing a quick

    
    
      for i in `seq 1 15`; do ssh www$i.website.com "reboot"; done
    

is VERY convenient. It's fast, quick to write, and extends existing shell
knowledge.

This would be very useful in all sorts of sysadmin places.

Imaging that I wrote a quick server, in Python/Rails/Node/whathaveyou, that
accepted input, then returned a formatted JSON array with server info.

Now, I can do

    
    
      $SERVERLIST = `curl http://my-server/list-of-servers`
      for i in ```$SERVERLIST.webservers.list()```; do ssh $i "reboot"; done
      for i in ```$SERVERLIST.dbservers.list()```; do ssh $i "uptime" >> serverload.txt; done

~~~
jsight
I've found that Perl works even better for this.

my @allfiles = `ls /somedir/`; for my $file (@allfiles) { chomp $file; ... }

Is a wonderful thing. It's not pretty Perl, but it's still better than Bash,
IMO.

~~~
raldi
Dude.

    
    
        my @allfiles = glob '/somedir/*';
    

Now you properly report errors _and_ you don't have to chomp.

------
jes5199
This is a start, but I'd like all my unix utils to input and output structured
data instead of walls of text

------
sramov
<https://github.com/rcrowley/json.sh>

------
manojlds
Powershell does! :)

~~~
itgoon
Yup. Posh ended my habit of installing cygwin, and now I cringe when I have to
do it the "old-fashioned" way on a Linux box.

Powershell is more verbose, but much more expressive.

------
figital
jsawk : <https://github.com/micha/jsawk>

~~~
catshirt
and a slimmer tool inspired by jsawk (in node)
<https://gist.github.com/1053262>

------
druiid
There are code modifications to bash to use PCRE and MySQL contexts. I mean,
these modifications are great and all... but I have yet to see the real point
behind them until they come standard in any distribution. Before that, they
are just play-toys since they are non-standard... IMHO.

------
tszming
Don't waste your time on this, as the author said:

"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."

------
harbud
I'd rather see an extensible quoting syntax a la Postgres' pl/pgSQL, e.g. ``j`
{"a": 1}``. When you want to support YAML: ``y` {a: 1}``.

------
ashayh
Things like this can be very useful on servers where its not possible to
install any Json parsing libraries.

~~~
iso8859-1
What do you mean? Using Python you can just place the library direction in the
working directory of your script. I don't see how this library is different
from any other library for other languages.

------
tlrobinson
Or XML! <https://gist.github.com/1074339>

------
wavephorm
At that point why not just write your scripts in Javascript, and run them with
nodejs?

~~~
gord
absolutely... Im imagining a 100% javascript shell. 'jash' ?

node.js is great for all those perl-y 1 to 10 liners. It should be the shell.

~~~
wavephorm
Maybe not a shell, but for simple scripts you can save your js code with the
following at the top:

    
    
      #!/usr/bin/env node
    

Then save the file as "myscript" and run it as any other executable. Node
already has a repl for experimentation.

~~~
gord
yes, and I do that often.

My point is that Javascript + Node would make the basis for a nice bash
replacement.

~~~
jebblue
If you like JavaScript. Java would be better. There is the Bean Shell. That
doesn't replace BASH for what it does well. In fact, that's the point of BASH,
not to replace traditional languages but to bind their output programs
together.

