Basho: Shell scripting and automation with plain JavaScript

jeswin
jeswin
Nov 29, 2017 · 5 min read
npm install -g basho

Spec and Documentation

# Prints 100 
basho -j 100
# Prints true
basho -j true
# Prints 100
basho -j 10**2
# This works
basho 100
# Prints 100
basho 10**2
# Prints nothing 
basho -p 100
# Prints hello, world 
basho '"hello, world"'
# Here's a better way to do this
basho -q hello, world
# Prints 10000 
basho 100 -j x**2
# Prints 1000. Escape the $. 
basho 1000 -e echo \${x}
# cat square.js 
module.exports = function square(n) { return n ** 2; }
# prints 100. Imports square.js as sqr.
basho 10 -i square.js sqr -j "sqr(x)"
# Prints 40000. Does sqr(10), then adds 100, then sqr(200)
basho 10 -i square.js sqr -j "sqr(x)" -j x+100 -j "sqr(x)"
# Prints 100 
echo 10 | basho parseInt(x)**2
# echo 1; echo 2; echo 3; echo 4 
basho [1,2,3,4] -e echo \${x}
basho "{ name: 'jes', age: 100 }" -e echo \${x.name}, \${x.age}
# echo kai; echo niki 
basho "[{name:'kai'}, {name:'niki'}]" -e echo \${x.name}
# echo 1 2 3; echo 3 4 5 
basho "[[1,2,3], [3,4,5]]" -e echo \${x[0]} \${x[1]} \${x[2]}
# echo 4 
basho [1,2,3,4] -a x.length -e echo \${x}
# echo 3; echo 4 
basho [1,2,3,4] -a "x.filter(x => x > 2)" -e echo \${x}
# echo 3; echo 4 
basho [1,2,3,4] -f x>2 -e echo \${x}
# Prints the sum 10 
basho [1,2,3,4] -a "x.reduce((acc,x)=>acc+x,0)" -e echo \${x}
# Prints the sum 10 
basho [1,2,3,4] -r acc+x 0 -e echo \${x}
# echo a1; echo b2; echo c3 
basho "['a','b','c']" -e echo \${x}\${i}
# echo 110 - which is (10^2) + 10 
basho 10 -j x**2 -e echo \${x} -j "parseInt(x)+10" -e echo \${x}
# Prints 100 
basho 10 x**2 | echo
# Prints 10 
basho "Promise.resolve(10)" -e echo \${x}
# Something more useful basho -i node-fetch fetch \
-j "fetch('http://oaks.nvg.org/basho.html')" \
-e echo \${x}

Advanced

# Prints [2,3,4,5]
basho [1,2,3,4] -j x+1 -j x+2 — stack 1
# Prints [2,3,4,5]
basho [1,2,3,4] -j x+1 -j x+2 — stack 1,2 -j x
basho --nostack ["a.txt", "b.txt"] -e cat \${x} -j x.length

Tip

# in .bashrc
alias basho='basho -i node-fetch fetch'
# now you could just do
basho "fetch('example.com/weather')" -j x.temperature

Real world examples

echo hello world hello hello | \
basho "(x.match(/hello/g) || []).length"
echo Bangalore,in | basho "fetch(\`http://api.openweathermap.org/data/2.5/weather?q=\${x}&appid=YOURAPIKEY&units=metric\`)" -j "x.json()" -j x.main.temp

jeswin

Written by

jeswin

I have an idea.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade