Going up a directory in babashka

447 Views Asked by At

I am trying to traverse the fp in babashka, and have found that running (shell "cd ..") in my script bb-test causes an error:

----- Error --------------------------------------------------------------------
Type:     java.io.IOException
Message:  Cannot run program "cd": error=2, No such file or directory
Location: /home/jack/Documents/clojure/leingit/./bb-test:120:1

Any ideas?

2

There are 2 best solutions below

0
On BEST ANSWER

You can't change the working directory in babashka (it is a limitation that comes from being a JVM-derived environment). But you can spawn new processes in other directories:

(require '[babashka.process :refer [shell]])

(shell {:dir ".."} "whatever")
0
On

cd is shell built-in; you can not call it outside a shell. So what you want to do instead is run your command with a changed "current working directory". This can be done in sh with the :dir option. E.g.

(-> (shell/sh "ls" :dir "/etc") :out)

This will list the content of /etc from wherever you are calling your script.

(as mentioned in the comments of the question, this is about calling git commands: git also allows for changing its root via the GIT_WORK_TREE env-var, which could be changed via the :env option from sh, but changing the cwd is more straightforward)