Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a vector of strings to java.shell in clojure?

I am trying to make use of (require [clojure.java.shell :refer [sh]]) to execute rsync backups in clojure. The function sh takes strings as args such as:

(sh "ls" "-las" "/home/")

However, I find myself with data arriving to sh as a vector of strings (e.g ["ls" "-las" "/home/"]) and this of course fails. This seems like what would be an extremely easy problem to solve, but I am just starting clojure (and lisp and functional programming by that matter) and I just can't seem to get this to work. How can I process the vector to pass to sh?

Any advice would be greatly appreciated.

like image 959
user1658878 Avatar asked Sep 08 '25 11:09

user1658878


1 Answers

It sounds like you need to use apply:

(apply sh arg-vector)

The apply function also pops up in other LISPs, allowing you to pass a list of arguments to a given function. In Clojure you're not limited to just lists, but any seq-able object, including vectors.

like image 67
DaoWen Avatar answered Sep 11 '25 03:09

DaoWen