How to pack a shell script in a leiningen project

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 could i jar the project so that the shell script is included if i load in clojars?


tracking

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

The 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 redirection ( > ) without increasing program memory consumption. The command (sh "bash" :in bash-command-string) allows my function to be written without a 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))) 
+4
source share
2 answers

Two steps:

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

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

+3
source

You really have two problems. First, how to put the file in a jar file. Secondly, how to access the file from the jar file.

The first is quite simple: include the file in the resource directory. All files in this directory are included in the jar file.

The second is harder, since sh will look for the script on disk, not in the jar file. You may need to extract the file using the loader.getresource class and write it to disk to execute it.

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

+2
source

Source: https://habr.com/ru/post/1434737/


All Articles