0

I want to take the output of simple.sh, a script from the internet and check its exit code. I on OSX

#!/bin/bash
$(curl -s http://127.0.0.1:8000/simple.sh)
if [ -z "$?" ]; then
    echo "Good"
    exit 0
else
    echo "Bad"
    exit 1
fi

simple.sh:

#!/bin/bash
exit 0

The problem I am getting is:

./test.sh: line 2: #!/bin/bash: No such file or directory
Bad
  • Try adding the full path to curl, i.e. the output from type curl. Probably a path issue. – datUser Mar 13 at 0:45
  • try eval "$(curl ...)" – Jonah Mar 13 at 1:57
  • @datUser Same problem still comes up. – Rice Mar 13 at 15:51
  • @Jonas Always returns "Good" no matter what the exit code from simple.sh is – Rice Mar 13 at 15:52
1

Can't say it's elegant, but this is the way I would do it:

#!/bin/bash
curl -s http://127.0.0.1:8000/simple.sh | /bin/bash -s >/dev/null 2>&1
rc=$?
if [ -z "$rc" ]
then
    echo "Good"
    exit 0
else
    echo "Bad"
    exit 1
fi


Seems to me, the way you are doing it is similar to executing a here-file inside the $( ... ) construct. Never tried that, not sure bash works that way.

Letting curl echo the contents of the file and piping it to bash accounts for the text output of the curl command and allows bash to execute it.

I'll bet that, if you try this, you will get the same results:

$( cat /[path]/simple.sh ); echo $?
  • 2
    I tested your script but as mentioned in another answer I had to change the if condition to [ $? -eq 0 ] and removed the unneeded rc variable. – user174174 Mar 13 at 5:23
  • It's debatable if rc=$? is actually necessary in this scenario. I do it out of habit, as many things I do actually do need it. – Scottie H Mar 13 at 5:51
  • 2
    I did the same as @user174174 and it works as expected! – Rice Mar 13 at 15:58
2

Your idea is right, but you seem to have defined a wrong conditional for checking the return code with [ -z "$?" ] which checks if the return code string is empty or not. Irrespective of the result of the curl output, your $? will carry a value which means, you'll never assert the if condition of your script. You probably need to check the return code that curl returns directly in your script

$(curl -s http://127.0.0.1:8000/simple.sh)
if [ "$?" -eq 0 ]; then

or even more tersely written without the test operator to allow the return code to be directly used in the if condition

if curl -s http://127.0.0.1:8000/simple.sh 2>&1 >/dev/null; then
  • 1
    if curl ...; then is true if curl fetches the web resource without errorif $(curl ...); then is the same as $(curl ...); if [ $? -eq 0]; then and is true if executing the tokens in the content of the web resource as a (simple) command does not cause any error. In the Q the first token received is apparently !#/bin/bash and executing that does normally cause an error. – dave_thompson_085 Mar 13 at 9:20
  • 1
    @Inian The first option you presented results in the same error. The second option unconditionally results in "Good" – Rice Mar 13 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.