curl从sed返回的字符串不可用

时间:2014-06-24 19:33:43

标签: bash url curl sed

我想从java样式属性文件中将主机名传递给curl。

this.properties

instance.dns=localhost  
...

bash命令

./ postFiles.sh this.properties

bash脚本

#!/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 'myFile=@DarthTests.jpg' $URL

两个代码块都回显相同的url:
http://admin:admin@localhost:8080/documentservice/api/doc

但是,如果我使用sed curl生成的URL无法解析主机

 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

为什么sed url在看起来完全一样时无法使用?

1 个答案:

答案 0 :(得分:2)

如果this.properties使用DOS样式的行结尾,那么您将HOST="localhost\r"

试试这个:

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