Last active
January 23, 2018 04:28
-
-
Save bacongravy/7f2dbbd9a20b0cfa547d3a618b5b0732 to your computer and use it in GitHub Desktop.
js-eval - Execute JavaScript, parse JSON, on vanilla macOS (no dev tools or packages installed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
alias js-eval="osascript -l JavaScript -e" | |
# examples: | |
js-eval "1+1" | |
# 2 | |
js-eval "Math.PI" | |
# 3.141592653589793 | |
JSON_STRING="[\"zero\", \"one\", \"two\"]" | |
js-eval "JSON.parse('$JSON_STRING')[1]" | |
# one | |
# advanced example: | |
# read from stdin and then parse as JSON | |
js-eval "stdin = $.NSString.alloc.initWithDataEncoding($.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile, $.NSUTF8StringEncoding).js; | |
JSON.parse(stdin)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment