Janet 1.40.1-1449ad8 Documentation
(Other Versions:
1.40.0
1.39.1
1.38.0
1.37.1
1.36.0
1.35.0
1.34.0
1.31.0
1.29.1
1.28.0
1.27.0
1.26.0
1.25.1
1.24.0
1.23.0
1.22.0
1.21.0
1.20.0
1.19.0
1.18.1
1.17.1
1.16.1
1.15.0
1.13.1
1.12.2
1.11.1
1.10.1
1.9.1
1.8.1
1.7.0
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
)
sh-dsl
A Domain specific language for creating shell commands and pipelines. This module complements
the spork/sh module with a terser tool for common operations. The general syntax is based
around the $ macro, which can be used to run commands and collect results in a manner that
very much looks like POSIX shell, but will also work on windows.
(use spork/sh-dsl)
# On posix systems, will use /bin/echo or similar to print "hello world!" and return 0, the exit code.
($ echo hello world!)
# To collect the stdout as a buffer result, use $<
($< echo hello world!)
# Same as $< but trims the last trailing newline
($<_ echo hello world!)Several basic shell features are supported with nice syntax:
- Basic commands
- Environment variables
- Redirecting stdio to/from files
- Janet expressions via unquote
- Pipes
- Windows Support
Environment Variables
Environment variables can be set for commands with the usual shell syntax of ENVVAR=VALUE preceding the program name. Environment variables from the calling process can also be passed in by passing in symbols that begin with a "$" character
(use spork/sh-dsl)
# Set the JANET_PATH and invoke the janet shell
($ JANET_PATH=/home/janet-user janet -e '(print (dyn *syspath*)))
# Also works
($ JANET_PATH=$HOME -e '(print (dyn *syspath*)))
($ echo $HOME)
($ echo ,(os/getenv "HOME")) # same thingKeep in mind that this syntax is not the same as Posix shell - environment variable expansion will not work everywhere and can always be substituted with inline Janet code instead.
Janet Expression Substitution
Users may want to mix Janet expressions with literals when invoking subprograms, and this library makes it easy
using the familiar unquote special.
(use spork/sh-dsl)
(def x 1)
# This will print "x"
($ echo x)
# This will print "1"
($ echo ,x)
# Both of these will print 6
($ echo ,(+ 1 2 3))
($ echo (+ 1 2 3))
# This will print "(+ 1 2 3)"
($ echo '(+ 1 2 3))Pipes
The input of one command can be piped into another as in shell scripts.
(use spork/sh-dsl)
($ ip address | grep docker)
(json/decode ($<_ curl -s https://www.githubstatus.com/api/v2/status.json | jq .))File Redirection
Standard IO (stdin, stdout, and stderr) can also be redirect to files much like the usual shell syntax.
(use spork/sh-dsl)
($ echo hi > out.txt) # Returns 0 and outputs nothing to the console - output has been redirected.
($ cat out.txt) # prints hi
($ janet -e '(error :oops) :err-to-out > out.txt) # redirect stderr to stdout
($ janet -e '(error :oops) :err-to-out >> out.txt) # redirect stderr to stdout and append to out.txt instead of overwriting
($ janet -e '(prin (:read stdin :all)) < out.txt) # reprint out.txtReference
sh-dsl/$ sh-dsl/$< sh-dsl/$<_ sh-dsl/$? sh-dsl/run-pipeline
($ & cmd) Run and return exit codes of each command in the pipeline. The last status code is the what is used generally for sucess/failure testing.
($< & cmd) Run and returns the standard output of the last command in the output
($<_ & cmd) Run and returns the standard output of the last command in the output with the last newline stripped.
(run-pipeline pipeline &named capture-output) Run a quoted pipeline object. Will return the exit code of all commands in the pipeline. If :capture-output is truthy, will also append the standard out of the last command in the pipeline.