Skip to content

Instantly share code, notes, and snippets.

@geekdenz
Created June 22, 2017 10:14
Show Gist options
  • Save geekdenz/f25b24476b00ebd988882cab3d5b17fd to your computer and use it in GitHub Desktop.
Save geekdenz/f25b24476b00ebd988882cab3d5b17fd to your computer and use it in GitHub Desktop.
Pipe your shell programs through JavaScript.
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
var f = process.argv[2];
rl.on('line', function(line){
var str = line.replace(/[\""]/g, '\\"');
eval('('+f+')("'+str+'")');
});
// usage: command | node pipe.js "(line) => function_body"
// e.g.
// df -h | node pipe.js '(s)=>{let spl = s.split(/\s+/); console.log(spl[spl.length-1])}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment