102 points · by e1ven · 14y ago · 49 comments
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.
$ 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/jsonpipeIt takes the idea of commands and pipes, but is oriented around python objects. Usable from the command line, and also has a python API.
It includes integration with the OS (e.g. commands to get files and processes as python objects), databases, remote nodes, and combinations of these, (E.g. submit a query to all nodes in a cluster, pull back the results and integrate them.)
I developed and used this tool while developing a sharded system running on a cluster of nodes with postgres running on each.
Object Shell is GPL. See the page above for examples, documentation and downloads.
Prepare to enter... the scary door.
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; doneI agree! If you do this a lot then you might be interested in GNU parallel. It's super handy!
seq 1 15 | parallel ssh www{}.website.com reboot
http://www.gnu.org/s/parallel/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.
my @allfiles = glob '/somedir/*';
Now you properly report errors and you don't have to chomp.Each tool has a purpose -- I don't think mucking around with json is particularly suitable for bash...
Don't use symmetrical quotes
Powershell is more verbose, but much more expressive.
"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."
node.js is great for all those perl-y 1 to 10 liners. It should be the shell.
#!/usr/bin/env node
Then save the file as "myscript" and run it as any other executable. Node already has a repl for experimentation.My point is that Javascript + Node would make the basis for a nice bash replacement.