jeremyjjbrown
jeremyjjbrown

Reputation: 8009

string returned from sed unusable by curl

I want to pass a hostname to curl from a java style properties file.

this.properties

instance.dns=localhost  
...

bash command

./postFiles.sh this.properties

bash script

#!/bin/bash

#blows up
HOST=$(grep  "^instance.dns=" ${1}| sed "s%instance.dns=\(.*\)%\1%")
URL="$(echo 'http://admin:admin@'${HOST}':8080/documentservice/api/doc')"
echo "$URL"

#works
HOST="$(echo 'localhost')"
URL="$(echo 'http://admin:admin@'${HOST}':8080/documentservice/api/doc')"
echo "$URL"

curl -v --globoff -F 'docKey=testFile0' -F 'fileType=IFP Paper Application' -F '[email protected]' $URL

Both code blocks echo the same url:
http://admin:admin@localhost:8080/documentservice/api/doc

However, if I use the URL generated by sed curl can not resolve the host

 Adding handle: conn: 0x7ff473803a00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7ff473803a00) send_pipe: 1, recv_pipe: 0
* Could not resolve host: localhost
* Closing connection 0
curl: (6) Could not resolve host: localhost

Why is the sed url unusable when it looks exactly the same?

Upvotes: 0

Views: 87

Answers (1)

glenn jackman
glenn jackman

Reputation: 247102

If this.properties uses DOS-style line endings, you'll have HOST="localhost\r"

Try this:

HOST=$( sed -n -e 's/\r$//' -e 's/^instance.dns=//p' "$1" )

Upvotes: 2

Related Questions