Fork me on GitHub
#babashka
<
2023-10-09
>
Adham Omran18:10:59

Hey all, I'm learning Babashka and I'm trying to translate the following command scanimage --format=tiff --device-name='airscan:e0:Canon MF230 (USB)' > test.tiff into bb I tried the following

(shell/sh
 {:out "./item.tiff"}
 "scanimage"
 "--format=tiff"
 "--device-name=airscan:e0:Canon MF230 (USB)")
But I'm getting
----- Error --------------------------------------------------------------------
Type:     java.lang.ClassCastException
Message:  java.lang.Object[] cannot be cast to java.lang.String[]
Location: /home/adham/pics/photo-album/scan.bb:1:1

----- Context ------------------------------------------------------------------
1: (shell/sh
   ^--- java.lang.Object[] cannot be cast to java.lang.String[]
2:  {:out "./item.tiff"}
3:  "scanimage"
4:  "--format=tiff"
5:  "--device-name=airscan:e0:Canon MF230 (USB)")
6: 

----- Stack trace --------------------------------------------------------------
clojure.java.shell/sh               - <built-in>
clojure.core/apply                  - <built-in>
babashka.impl.clojure.java.shell/sh - <built-in>
user                                - /home/adham/pics/photo-album/scan.bb:1:1
I'm following the https://book.babashka.org/#_shell section on how to output. I also tried {:out "item.tiff"} with the same result. I do have to note that (shell/sh {:out "file.txt"} "echo hello") also does not work while (shell/sh "echo" "hello") does.

Bob B18:10:07

based on the stack trace, it appears that the script is using clojure.java.shell, which a different API from the shell funciton in the task API or babashka.process/shell, so you might want to either use the babashka.process version (since it's presumably a script and not a task), or have a look at the docs for clojure.java.shell/sh

borkdude18:10:15

yeah, use babashka.process , it looks like that is what you wanted based on your arguments

Adham Omran18:10:06

Success! Thank you both I've had issues since the book mentions (shell) as the function in most examples when it is stated that shell is an alias for clojure.java.shell. Was I supposed to read the symbol shell differently in this section of the book?

borkdude18:10:36

@U03QTHYKXK7 An alias is not the same as a function name, an alias in Clojure is used to abbreviate a namespace name

Adham Omran18:10:22

Yes, I understand that, the idea is to run something like (shell/sh) instead of (clojure.java.shell/sh). My point and perhaps I was not clear is that copying the code sample in the Shell section which is

(shell {:out "file.txt"} "echo hello")
Throws an exception the with the following
Type:     clojure.lang.ExceptionInfo
Message:  Could not resolve symbol: shell
My question now is, was I not supposed to copy from the book?

borkdude18:10:13

@U03QTHYKXK7 Did you read the whole chapter or just one section from the book? That snippet works in babashka tasks but not in a random file

borkdude18:10:41

When you are just putting some code in a file, the full example would be:

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

Adham Omran18:10:39

Oh. Sorry that's on me, I did not read it in context. I should really sit down and go start to finish instead of Ctrl-F my way through it Thank you again, looking to use bb in prod soon

🎉 1
Adham Omran18:10:47

Quite the resource, I'll give it a read tomorrow!