1

I have below .sh file

// ping.sh 01: port="$1" 02: echo "pinging http://localhost:$port/ping" 03: 04: retry=0 05: while 06: sleep 1 07: api_response=$(curl --write-out %{http_code} --silent --output /dev/null "http://localhost:$port/ping") 08: echo "resp $api_response" 09: (( "$api_response" != "200" && ++retry < 100 )) 10: do :; done 

When I run it directly with ./ping.sh 8080, it works fine. However, when I run it with shelljs it fails with below error.

// ping.js require('shelljs').exec("./ping.sh 8080", {async:true}); $ node ping.js pinging http://localhost:8080/ping resp 000 ./ping.sh: 9: ./ping.sh: 000: not found 
3
  • it looks like the (( )) syntax is not supported in shelljs, i think this only works in bash Commented May 5, 2018 at 23:14
  • I converted it to a for loop but now I get same error in this line if [ "$api_response" == "200" ]; then. I am afraid I will switch to child_process instead of shelljs Commented May 5, 2018 at 23:27
  • why not a library like this? github.com/danielzzz/node-ping Commented May 5, 2018 at 23:44

1 Answer 1

1

It does work , simply add #!/bin/bash to ping.sh


Working proof-of-concept :

ping.sh

#!/bin/bash port="$1" echo "pinging http://localhost:$port/ping" retry=0 while sleep 1 api_response=$(curl --write-out %{http_code} --silent --output /dev/null "http://localhost:$port/ping") echo "resp $api_response" (( "$api_response" != "200" && ++retry < 100 )) do :; done 

ping.js

require('shelljs').exec("./ping.sh 8080", {async:true}); 

terminal output :

node ping.js pinging http://localhost:8080/ping resp 000 resp 000 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.