Linux shell脚本 – 是否有可能捕获命令的输出并在另一个命令中使用它

所以我想自动将file upload到Rackspace。 以下是我需要做的事情:

首先,我input:

curl -D - \ -H "X-Auth-Key: 123456789abcdefghijk" \ -H "X-Auth-User: MyUsername" \ https://auth.api.rackspacecloud.com/v1.0 

这给了我一个回应,例如:

 HTTP/1.1 204 No Content Server: Apache/2.2.3 (Red Hat) vary: X-Auth-Token,X-Auth-Key,X-Storage-User,X-Storage-Pass X-Storage-Url: https://storage101.ord1.clouddrive.com/v1/BlahBlah Cache-Control: s-maxage=46818 Content-Type: text/xml Date: Tue, 07 Feb 2012 05:07:09 GMT X-Auth-Token: SOME_AUTH_TOKEN_I_NEED X-Storage-Token: blahblah X-Server-Management-Url: https://servers.api.rackspacecloud.com/v1.0/123456 Connection: Keep-Alive X-CDN-Management-Url: https://cdn2.clouddrive.com/v1/BlahBlah 

接下来,我需要上传一个文件:

 curl -X PUT -T foo.txt -D - \ -H "ETag: 7849eb8d56581fa7c4896bb0db64892c" \ -H "Content-Type: text/plain" \ -H "X-Auth-Token: SOME_AUTH_TOKEN_I_NEED" \ -H "X-Object-Meta-Screenie: Test" \ https://storage101.ord1.clouddrive.com/v1/BlahBlah/MyBackup/foo.txt 

所以基本上,我需要parsingauth令牌(X-Auth-Token)以及存储URL(X-Storage-Url)并dynamic构build下一个命令。

有没有办法做到这一点与shell脚本,也许使用正则expression式或什么? 任何指针将不胜感激!

肯定有一个比这个更优雅的解决scheme,但是从我的头顶来看,一个快速和肮脏的bash的方式是这样的:

 #!/bin/bash xauthtoken=`curl -D - \ -H "X-Auth-Key: 123456789abcdefghijk" \ -H "X-Auth-User: MyUsername" \ https://auth.api.rackspacecloud.com/v1.0 | grep X\-Auth\-Token | awk '{print $2}'` xstorageurl=`curl -D - \ -H "X-Auth-Key: 123456789abcdefghijk" \ -H "X-Auth-User: MyUsername" \ https://auth.api.rackspacecloud.com/v1.0 | grep X\-Storage\-Url | awk '{print $2}'` curl -X PUT -T foo.txt -D - \ -H "ETag: 7849eb8d56581fa7c4896bb0db64892c" \ -H "Content-Type: text/plain" \ -H "X-Auth-Token: ${xauthtoken}" \ -H "X-Object-Meta-Screenie: Test" \ $xstorageurl