Giter VIP home page Giter VIP logo

Comments (8)

rosineygp avatar rosineygp commented on July 28, 2024 1

Hi @chr15m

The following function helps me to get exit code, for stderr I guess that needs to create a tmp file... 😞

but it, helps me a lot.

(defn sh! [command]
  (let [cmd (str command "; echo «$?")
        result (str-split (str-replace (sh* cmd) "\n" "") "«")]
        (hash-map :out (str-replace (get result 0) "" "\n") :exit (get result 1))))

(def ls (sh! "ls -la"))
(println (get ls :out))
(println (get ls :exit))

(def err (sh! "laaaaa"))
(println (get err :out))
(println (get err :exit))

Thanks!

from flk.

rosineygp avatar rosineygp commented on July 28, 2024 1

Ugly as f**k but works

(defn sh! [command]
  (let [uuid (str "/tmp/" (sh* "uuidgen"))
        cmd (str command " 2> " uuid "; echo «$?")
        result (str-split (str-replace (sh* cmd) "\n" "") "«")
        err (sh* (str "cat " uuid))]
    (sh* (str "rm " uuid))
    (hash-map :out (str-replace (get result 0) "" "\n") 
              :err err 
              :exit (read-string (get result 1)))))

(println (sh! 'ping ["goober.com"]))

(def ls (sh! "ls -la"))
(println (get ls :out))
(println (get ls :err))
(println (get ls :exit))

(def err (sh! "laaaaa"))
(println (get err :out))
(println (get err :err))
(println (get err :exit))

I guess uuidgen is default at ubuntu, a generic uuid can be used for safe files
https://github.com/dylanaraps/pure-bash-bible#generate-a-uuid-v4

escapes chars like ¶ and « should be changed to something better.

from flk.

chr15m avatar chr15m commented on July 28, 2024

This will probably require a bash level replacement for sh* in extras.sh because the existing implementation wraps the call in such a way that it's impossible to get at the return code.

from flk.

chr15m avatar chr15m commented on July 28, 2024

What I was thinking of is using the catch function found here:

https://stackoverflow.com/questions/11027679/capture-stdout-and-stderr-into-different-variables/41069638#41069638

Just inlining that function into flk's bash functions and then writing a small wrapper to use it.

from flk.

chr15m avatar chr15m commented on July 28, 2024

This technique seems to work better:

https://stackoverflow.com/questions/11027679/capture-stdout-and-stderr-into-different-variables/18086548#18086548

PR coming through.

from flk.

chr15m avatar chr15m commented on July 28, 2024

Fixed on branch issue-12-sh-interop. @rosineygp please let me know if this solves your issue.

from flk.

rosineygp avatar rosineygp commented on July 28, 2024

Fixed on branch issue-12-sh-interop. @rosineygp please let me know if this solves your issue.

Works!!! 😉

I just expected the return as hash-map... like clojure https://clojuredocs.org/clojure.java.shell/sh, but it's not a real problem.

The bash solution is ugliest of my code... but works better 😃 stderr and stout without create a tmp file is amazing.

Thanks @chr15m

I will try to implement last reverse and other small clojure functions if it works fine all will make a PR.

from flk.

chr15m avatar chr15m commented on July 28, 2024

Oh, that hash-map thing would be great. 👍 Thanks for testing!

from flk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.