Shell scripting
April 7, 2019 ยท View on GitHub
The closh-zero.jar binary accepts same arguments as clojure.main CLI. That includes all features like aliases and deps. You can refer to the options with help:
closh-zero.jar --help
Running scripts
The most common way is running a one-off script by passing it as a command line argument like:
closh-zero.jar my-script.clj
Another common way is using the shebang:
#!/usr/bin/env closh-zero.jar
ls (first *args*)
Then you can make the script executable and run it like:
chmod +x my-script.clj
./myscript.clj
Writing scripts
Top-level commands can be written using the shell syntax:
#!/usr/bin/env closh-zero.jar
echo "Working..."
sleep (rand-int 5)
echo "Done"
When you need to need to use the commands inside conditionals or functions, wrap them in a sh macro. Individual commands need to be separated with \;.
#!/usr/bin/env closh-zero.jar
(defcmd do-work [interval]
(sh echo "Working..." \;
sleep (str interval) \;
echo "Done"))
do-work (rand-int 5)
Using arguments
To use CLI arguments you can use the *args* alias, which is a sequence of arguments passed by user.
#!/usr/bin/env closh-zero.jar
echo "Number of arguments:" (count *args*)
For parsing command line arguments tools.cli works great. It is also possible to use any other library.
Examples
As an example I ported gifgen utility to closh. Compare the original bash version with the closh version.