4

I have a shell script saved in scripts/shell/record_video.sh that I need to call in one of my projects.

An example of its use in code:

(defn save-mp4 [cam filename duration]
  (sh "scripts/shell/record_video.sh" (:i_url cam) filename (str duration)))

how would I be able to jar the project so that the shell script is included if i upload to clojars?


follow up

Thanks @Ankur. The (sh "bash" :in bash-command-string) is super useful. The only reason I needed the .sh file in the first place was because I couldn't quite figure out how to do redirects when the stdout contains something big (like a video).

the file scripts/shell/record_video.sh contains:

SRC=rtsp://$1:554/axis-media/media.amp?resolution=1920x1080
DST=$2
LEN=$3
openRTSP -4 -d $LEN -w 1440 -h 1080 -f 25 -u root pass $SRC > $DST 2> /dev/null

and I did not know how to translate the redirect (>) without making the program memory consumption enormous. The (sh "bash" :in bash-command-string) command allows my function to be written without the shell script:

(defn save-mp4 [cam filename duration]
  (let [src (format "rtsp://%s:554/axis-media/media.amp?resolution=1920x1080" (:i_url cam))
        cmd (format "openRTSP -4 -d %d -w 1440 -h 1080 -f 25 -u root pass %s > %s 2> /dev/null"
                    duration src filename)]
    (sh "bash" :in cmd)))
zcaudate
  • 13,998
  • 7
  • 64
  • 124

2 Answers2

3

Two steps:

  • Package the shell script as resource.
  • Read the shell script file using java resource API and use (sh "bash" :in file-str)

Where file str is the shell script content read using resource API.

Ankur
  • 33,367
  • 2
  • 46
  • 72
  • The `:in` string is passed to standard input of the process that you are starting.. hence this basically start bash and then write the script code to bash stdin and hence bash will execute that script.. something like you do `echo 'echo hello' | bash` – Ankur Sep 18 '12 at 08:46
2

You actually have two problems. First how to put a file into a jar file. Second, how to access the file from within the jar file.

The first is simple enough: include the file on the resources directory. All files in that directory are included in the jar file.

The second is a more difficult as sh is going to be looking for the script on disk, not nestled in a jar file. You may have to extract the file using class loader.getresource and them write it to disk to execute it.

There is a discussion on how to read a resource from a jar with the simplest being (clojure.java.io/resource "myscript.js")

Community
  • 1
  • 1
M Smith
  • 1,988
  • 15
  • 28