字符串附加在linux shell脚本中

时间:2013-10-10 05:51:57

标签: linux sh

我编写Linux shell脚本来测试上传文件。

在我们将文件上传到存储系统之前我需要使用其他全服务获取存储网址,获取网址成功。在存储网址中我想附加一个字符串。

Url=$(grep "X-Storage-Url" swiftAuth.txt)  //read url value from file
storageFileUrl=${storageUrl#*:}  //remove extra string before :
echo $storageFileUrl"/111" 

url就像https://cloudstorage.com/auth/1.0

所以当我试图在运行时追加字符串时,它给了我错误的价值 结果是:

/111s://cloudstorage.com/auth/1.0 

以上结果是错误的我想要结果

https://cloudstorage.com/auth/1.0/111

请帮帮我

感谢。

1 个答案:

答案 0 :(得分:2)

不,你没有得到/111s://cloudstorage.com/auth/1.0。您将获得https://cloudstorage.com/auth/1.0␍/111

是回车,也称为Ctrl-M或^M

Bash认为是常规角色。您应该只需${storageFileUrl%␍}即可剥离它。您可以使用组合 Ctrl-V Ctrl-M 在命令行,vim或emacs中键入

或者您可以使用recodedos2unix或类似工具来删除初始grep输入中的.s。

或者你可以使用sed将grep和stripping组合成一个步骤,并且也可以为你处理((使用URL不能包含空格的事实):

storageFileUrl=$(sed -n 's/X-Storage-Url:[ \t]*\([^ \t\r]*\).*/\1/p')

注意:MIME指定标题的␍␊行结尾,而Unix只使用␊,所以有望在那里看到.s。