CURL in bash script works with . (dot space), not ./ (dot slash)

0

I have a bash script which sends a couple of notifications to Slack via CURL and spawns an expect script.

When I run the script via . scriptName.sh it works fine, but when I run it by typing ./scriptName.sh the CURL portions fail with the error message:

protocol $https not supported or disabled in libcurl

Here's the basic file:

curl --data "building Dev" $"https://domain.slack.com/services/hooks/slackbot?token=yourTokenHere&channel=%23yourChannelHere"

expect script.exp

curl --data "Dev built" $"https://domain.slack.com/services/hooks/slackbot?token=yourTokenHere&channel=%23yourChannelHere"

General_Twyckenham

Posted 2015-10-19T16:00:26.087

Reputation: 87

2Is your script executable? If not run chmod a+x scriptName.sh. The . command requires only that a script be readable. – AFH – 2015-10-19T16:04:22.243

@AFH Duh I feel like a moron. I actually thought of that, but my coworker said he did that already :/ Thanks!!! – General_Twyckenham – 2015-10-19T16:05:45.857

Did making the script executable fix anything? That's surprising to me, as if the script wasn't executable, bash wouldn't have run it so it wouldn't even get to the curl. My guess is that the https environment variable hasn't been exported. . scriptName.sh runs the script in the same shell, so the script sees all environment variables. ./scriptName.sh runs the script in a subshell, so it only sees exported environment variables. Try export https;./scriptName.sh and see if that changes anything. – blm – 2015-10-19T16:33:21.887

Why is there a $ before the URL? In bash, this invokes string localization; in other shells,... who knows? Speaking of which, your script really should start with a shebang line, like #!/bin/bash. – Gordon Davisson – 2015-10-20T03:19:54.793

@blm Thanks for the advice. According to AFH's earlier comment, the . seems to execute the file even if it's only readable. Making the script executable did fix my issue, but I would have tried your suggestions had it not worked. – General_Twyckenham – 2015-10-20T14:49:22.577

@GordonDavisson I will add the shebang. I don't remember where the $ came from, possibly from the Slack API docs, but it seems to work properly when the file is permissioned properly – General_Twyckenham – 2015-10-20T14:50:40.490

Answers

0

So the script works fine when you "source" it (by using ". command"), but not when you execute it (using "./command").

The first thing to check is permissions. Make sure it has executable permissions.

The other thing that I've found, that frequently fixes this type of situation, is to mark the file as a script, by inserting the following as the very top line of the file:

#!/bin/sh

Of course, if you use any shell-specific features, you should specify which alternate shell to use.

TOOGAM

Posted 2015-10-19T16:00:26.087

Reputation: 12 651