dundalek/closh

Usage: exit codes, experience report

Closed this issue · 3 comments

(using the uberjar)

Ok so I've been trying to write some bash with some retry logic. I've failed, and now I'm trying to do this with closh

In this particular case I want to check if there is a server running on a particular port.

My first thought is to do something like:

curl --silent localhost:80; (if (= $? 0) (do (println "success") (exit 0)) (do (println "failure") (exit 1))

This doesn't work as there is no $? or *1 and ; doesn't seem to be part of closh.

So after having a first look at the docs (yep), I see that there is a sh-ok function:

(if (sh-ok curl --silent localhost:80)
   (do (println "success") (exit 0)) 
   (do (println "failure") (exit 1)))

This sort of works, but it exit's the closh shell. I want this to be part of an environment where exit codes matter (in this particular case AWS Codebuild) and so I want to communicate success/failure that way.

Another unsuccesful try that also exits

$ (if (sh-ok curl --silent localhost:80)
    (do (println "success") (exit 0))
    (do (println "failure") (exit 1))) && echo "foo" || echo "bar"

Am I approaching this problem the wrong way? Apologies if I'm missing obvious stuff

I think your second solution with (if (sh-ok ... is how I would do it. I think having the exit codes is related to the script mode #96. After the script mode is implemented this should become straightforward.

@jeroenvandijk It might be worth to give this a shot with the new script mode release. I am not sure what the is the issue here. If you post the code in bash, I can help to translate it for closh.

@dundalek I think I was approaching the problem in the wrong way. With Closh I actually don't need to rely on the bash exit codes:

(if (sh-ok curl --silent localhost:80)
  (do (println "success") (println "foo"))
  (do (println "failure") (println "bar")))

I'll close the issue