Babashka in Nextjournal 101 Demo

# question: Is there a human readible history of Babashka somewhere I can read?
# outdated version of Babashka
# wget https://github.com/borkdude/babashka/releases/download/v0.2.5/babashka-0.2.5-linux-amd64.zip
2.4s
Babashka (Bash)
# outdated version of Babashka
# unzip -o babashka-0.2.5-linux-amd64 -d /usr/local/bin
# rm babashka-0.2.5-linux-amd64.zip
1.3s
Babashka (Bash)

Skill: Download Babashka source (?)

# import the newest version of Babashka
# wget https://github.com/babashka/babashka/releases/download/v0.4.0/babashka-0.4.0-linux-amd64.tar.gz
0.8s
Babashka (Bash)

Skill: Unzip Babashka source

# question: What needs to be changed here to correctly unzip babashka?
# tar -xvzf -o babashka-0.4.0-linux-amd64 -d /usr/local/bin
0.7s
Babashka (Bash)
tar --help
1.5s
Babashka (Bash)
bb --version
3.4s
Babashka (Bash)

Skill: Delete a file

# Not sure that I want to call this a skill... I'm removing the source zip... but I could be using Babashka at this point to do so!
# rm babashka-0.4.0-linux-amd64.tar.gz
# TODO: rewrite the above line in Babashka
# STUB: remove the babashka source zip by using Babashka
0.9s
Babashka (Bash)

Babashka Book Study, Session 001

Babashka is a scripting environment made with Clojure

Question: What are the primary use cases for scripting and scripting environments? File manipulation, text editing, etc.?

Babashka comes with batteries included and packs libraries like clojure.tools.cli for parsing command line arguments and cheshire for working with JSON.

Question: Can further context be provided for what "with batteries included" means, along with counter examples?

Babashka uses sci for interpreting Clojure. Sci implements a substantial subset of Clojure.

Question: What are the key differences and gotchas between Sci and the core functionalities of Clojure?

Section 001 "Running Babashka" START

Skill: Run an expression "in-line" (?)

# question: What does the `-e` flag stand for? Execute, evaluate, expression... ?
# answer: The `-e` flag stands for "evaluate"
#         From the help menu "-e, --eval <expr> Evaluate an expression."
# description: "The babashka executable is called bb."
bb -e '(+ 1 2 3)'
1.2s
Babashka (Bash)
# "I’m looking for replacements to mv, rm, and cp. I don’t want to do this with clojure.java.shell; in that case, I would just write the bash script. Can I do it in an OS-agnostic way in Babashka?"
# source: https://clojureverse.org/t/how-to-replace-every-day-bash-commands-with-babashka/6459/3?u=avidrucker
echo "babashka" > /tmp/a
bb '(io/copy (io/file "/tmp/a") (io/file "/tmp/b"))'
cat /tmp/b
1.0s
Babashka (Bash)

Skill: Move a file in one step without copying or deleting

# "What about bash mv? Is there a way without doing the copy and adding a delete?"
# source: https://clojureverse.org/t/how-to-replace-every-day-bash-commands-with-babashka/6459/5?u=avidrucker
bb '(.renameTo (io/file "/tmp/a") (io/file "/tmp/moved"))'
cat /tmp/moved
1.5s
Babashka (Bash)

Skill: Save code as text into a CLJ file

echo "(+ 5 6 7)" > /tmp/script.clj
1.5s
Babashka (Bash)

Skill: Run code from a CLJ file

# "the -f flag is optional when the argument is a filename"
# from the help menu: "-f, --file <path> Evaluate a file."
bb /tmp/script.clj
bb -f /tmp/script.clj # same as above line
1.8s
Babashka (Bash)

Skill: Run code from script with shebang invoked by filename only

# Commonly, scripts have shebangs so you can invoke them with their filename only:
# create and write to a file called script_b.clj
# script_b.clj
echo "#!/tmp bb
(+ 1 2 3)" > /tmp/script_b.clj
1.2s
Babashka (Bash)
# confirm that the contents are as expected
cat /tmp/script_b.clj
1.3s
Babashka (Bash)
# skill: run code from script with shebang by invoking filename only
bb /tmp/script_b.clj
1.1s
Babashka (Bash)

Section 001 "Running Babashka" END

Section 002

Skill: Get some help within Babashka

# skill: print the help text
bb -h
1.2s
Babashka (Bash)

Skill: Identify what a Babashka command is doing, and to inspect when unsure

"If neither -e, -f, or --socket-repl are specified, then the first argument that is not parsed as a option is treated as a file if it exists, or as an expression otherwise. Everything after that is bound to *command-line-args*."

# question: why does this result in error?
# bb repl
# note: careful! this will never terminate and the code cell is stuck!
13.6s
Babashka (Bash)
# question: why does this result in error?
# answer: `doc` requires an argument
# question: Is there a valid argument for `doc` to take in this notebook as it stands currently?
# bb doc
0.9s
Babashka (Bash)
# question: why does this result in error?
bb describe
# question: What is the (meaning/significance of the) following map?
1.4s
Babashka (Bash)
# I am trying to make an EDN or Markdown cell here, but
# typing "```markdown " or "```edn " doesn't result in those types of cells, they simply just turn into another Babashka cell
# Question: What am I do doing incorrectly here?
Babashka (Bash)
# question: why does this result in error?
# answer: This results in error because running Clojure requires Java which doesn't exist in Nextjournal. 
# question: What is a minimum viable example of `bb clojure` used in context?
# bb clojure
1.0s
Babashka (Bash)
bb --version
2.1s
Babashka (Bash)

More coming soon...

Next Step

Runtimes (1)