Fork me on GitHub
#clojure
<
2019-01-12
>
jdkealy00:01:52

i've got a pretty lengthy bash command i'd like to execute in clojure. Is there any way i can just execute the full command with all its arguments as a single string?

noisesmith00:01:06

sure, via (clojure.java.shell/sh "/bin/bash" "-c" cmd) for example

noisesmith00:01:25

you can do a similar trick with ProcessBuilder if you need more flexibility about how the command runs

noisesmith00:01:54

shell/sh itself doesn't invoke the OS shell - for one thing that's not portable

noisesmith00:01:21

@jdkealy

ins)user=> (.start (ProcessBuilder. (into-array ["/bin/bash" "-c" "ls"])))
#object[java.lang.UNIXProcess 0x2a8d39c4 "java.lang.UNIXProcess@2a8d39c4"]
(ins)user=> (slurp (.getInputStream *1))
"CHANGELOG.md\nDockerfile\nREADME.md\nTAGS\nbin\ndashboards\ndeploy\ndev-resources\ndoc\ndoc-updates.otl\ndocker\ndocker-compose.yml\neastwood-config.clj\nprofiles.clj\nproject.clj\nresources\nsrc\ntarget\ntest\n"
(ins)user=> (require 'clojure.java.shell)
nil
(ins)user=> (clojure.java.shell/sh "/bin/bash" "-c" "ls")
{:exit 0, :out "CHANGELOG.md\nDockerfile\nREADME.md\nTAGS\nbin\ndashboards\ndeploy\ndev-resources\ndoc\ndoc-updates.otl\ndocker\ndocker-compose.yml\neastwood-config.clj\nprofiles.clj\nproject.clj\nresources\nsrc\ntarget\ntest\n", :err ""}

jdkealy00:01:53

ok my issue is parsing out the arguments into an array like that. I tried using conch. And i'm trying to use ffmpeg. this command returns a non-zero exit code at the first step but also returns the info i want, but conch just hangs up at the first error

ffmpeg -i /Volumes/Video 1/DRUM_2018-12-19-15-00-54_597.mov 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//

noisesmith00:01:13

yeah, just use ProcessBuilder instead of conch

noisesmith00:01:42

you need to explicitly call bash or sh to get things like |

noisesmith00:01:57

those aren't execution features, they are shell features, so you need to start a shell

noisesmith00:01:28

(so maybe conch is still OK for you, as long as you start a shell - I found ProcessBuilder much easier to use in anger)

jdkealy01:01:31

haha... so like

(def output (.start (ProcessBuilder. (into-array ["/bin/bash" "-c"  "ffmpeg -i /Volumes/Video 1/DRUM_2018-12-19-15-00-54_597.mov 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//"]))))

noisesmith01:01:06

yeah, but that's more than output - it is a Process object that lets you read stdout, stderr, send to stdin, etc.

noisesmith01:01:50

in my experience for interactive commands like ffmpeg if I want to send it input lines mid execution etc. Process directly instead of conch was easier

jdkealy01:01:51

wow much easier! Thanks!

noisesmith01:01:00

glad I could help

noisesmith01:01:16

also, once you have an InputStream you are free to parse each line of input from the process with your own parser if sed and cut aren't "cutting it", so to speak

😂 5
noisesmith01:01:31

but if sed and cut work, no need to reinvent that

Andres01:01:52

Anybody know how to use missing? with a reverse lookup? Is this supposed to work? [(missing? $ ?l :account/_licenses)] Also, is there a reason why I can't use 2 missing? statements in an or? (or [(missing? $ ?a :account/licenses)] [(missing? $ ?a :account/email)]) (edited) Error CompilerException java.lang.RuntimeException: Unable to resolve symbol: $ in this context

noisesmith01:01:33

what is "missing?"

jdkealy01:01:03

you mean that for the #datomic channel "?

noisesmith01:01:29

oh, if it's datomic the who vector should be quoted right?

jdkealy01:01:25

yeah... needs to be quoted

Andres01:01:28

There's more exposure here... but I will politely retreat back to #datomic if someone protests me posting here.

chrisulloa04:01:08

Something I discovered today is that YourKit works really well with Clojure in an IntelliJ setup (doesn’t interfere with Cursive).

chrisulloa04:01:23

For profiling Clojure code!

chrisulloa04:01:35

Figuring out what Java class names correspond to which parts of your Clojure code is kind of difficult though. But otherwise really helpful once you figure out what’s what.

Chris09:01:08

What’s the best way to interop with Java methods that require a java.util.function? I know it can be done with reify but it is a bit verbose.

dominicm11:01:54

There's a library around which provides a macro for that.

jradek12:01:52

Hi, I have a question about quil library. How to I call individual functions in the REPL, e.g. quil.core/random? Most quil function can only be run inside sketch functions (I think because of state handling). Is the any way to grab the current state in the repl and execute code?

gncgnc12:01:52

You'll have better luck asking in the dedicated #quil channel

jradek13:01:59

Thanks. didn't know of it