0

I am running bash script with curl command in it. I read the username, password and url from a properties file. But when the command gets executed, the response gives me an error with saying you are not authorized to access this page. I print out the whole curl command and it is correct.

Here is the part of the code:

this gives me response saying not authorized:

curl -u $USERNAMEVAL:$PWDVAL $URLVAL

this works perfectly even from inside bash program:

curl -u test\\test:test www.blah.com

What could be the problem? Any help will be appreciated.

3
  • you may want to add the fact you are using NTLM. ;auth=NTLM Commented Nov 21, 2013 at 13:48
  • This could be related to the quoting of the backslashes. Could you show us how you assign values to $USERNAMEVAL? Commented Nov 21, 2013 at 13:54
  • I add the value to the $USERNAMEVAL by reading from a properties file in the follwing way: for line in cat webservice.properties| grep -v ^# do name=$(echo $line | awk -F\= '{print $1}') value=$(echo $line | awk -F\= '{print $2}') #echo $name=$value if [ "$name" == "$URL" ]; then URLVAL=$value fi if [ "$name" == "$USERNAME" ]; then USERNAMEVAL=$value fi if [ "$name" == "$PWD" ]; then PWDVAL=$value fi done Commented Nov 21, 2013 at 14:19

1 Answer 1

1

When I have a situation where a command in a Bash shell script fails yet the same command with what I think are the same parameters works when I manually run it, I suspect an issue with either the parameter values or the parameter quoting.

As an initial step in isolating the problem, I'd suggest that immediately before the line

curl -u $USERNAMEVAL:$PWDVAL $URLVAL

you add something such as

echo "curl -u $USERNAMEVAL:$PWDVAL $URLVAL"

Or even (to be very clear):

echo "\$USERNAMEVAL(${#USERNAMEVAL}): \"$USERNAMEVAL\""
echo "\$PWDVAL(${#PWDVAL}): \"$PWDVAL\""
echo "\$URLVAL(${#URLVAL}): \"$URLVAL\""
echo "curl -u $USERNAMEVAL:$PWDVAL $URLVAL"

This prints the name, length, and value of each of the variables being used to construct the curl command.

E.g.:

#!/bin/bash
USERNAMEVAL='test\\test'
PWDVAL='test'
URLVAL='www.blah.com'

echo "\$USERNAMEVAL(${#USERNAMEVAL}): \"$USERNAMEVAL\""
echo "\$PWDVAL(${#PWDVAL}): \"$PWDVAL\""
echo "\$URLVAL(${#URLVAL}): \"$URLVAL\""
echo "curl -u $USERNAMEVAL:$PWDVAL $URLVAL"

This prints:

$USERNAMEVAL(10): "test\\test"
$PWDVAL(4): "test"
$URLVAL(12): "www.blah.com"
curl -u test\\test:test www.blah.com
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.