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
4
  • Try adding the full path to curl, i.e. the output from type curl. Probably a path issue.
    – 111---
    Commented Mar 13, 2019 at 0:45
  • try eval "$(curl ...)"
    – Yunus
    Commented Mar 13, 2019 at 1:57
  • @datUser Same problem still comes up.
    – Rice
    Commented Mar 13, 2019 at 15:51
  • @Jonas Always returns "Good" no matter what the exit code from simple.sh is
    – Rice
    Commented Mar 13, 2019 at 15:52

2 Answers 2

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
2
  • 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. Commented Mar 13, 2019 at 9:20
  • 1
    @Inian The first option you presented results in the same error. The second option unconditionally results in "Good"
    – Rice
    Commented Mar 13, 2019 at 15:57
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 $?
3
  • 3
    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
    Commented Mar 13, 2019 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
    Commented Mar 13, 2019 at 5:51
  • 2
    I did the same as @user174174 and it works as expected!
    – Rice
    Commented Mar 13, 2019 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .