当我回声时,我得到了它,当我将其输入到终端时,它将运行
curl -i \ -H "Accept: application/json" \ -H "Content-Type:application/json" \ -X POST --data '{"account":{"email":"akdgdtk@test.com","screenName":"akdgdtk","type":"NIKE","passwordSettings":{"password":"Starwars1","passwordConfirm":"Starwars1"}},"firstName":"Test","lastName":"User","middleName":"ObiWan","locale":"en_US","registrationSiteId":"520","receiveEmail":"false","dateOfBirth":"1984-12-25","mobileNumber":"9175555555","gender":"male","fuelActivationDate":"2010-10-22","postalCode":"10022","country":"US","city":"Beverton","state":"OR","bio":"This is a test user","jpFirstNameKana":"unsure","jpLastNameKana":"ofthis","height":"80","weight":"175","distanceUnit":"MILES","weightUnit":"POUNDS","heightUnit":"FT/INCHES"}' https://xxx:xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx
但是在bash脚本文件中运行时,出现此错误
curl: (6) Could not resolve host: application; nodename nor servname provided, or not known curl: (6) Could not resolve host: is; nodename nor servname provided, or not known curl: (6) Could not resolve host: a; nodename nor servname provided, or not known curl: (6) Could not resolve host: test; nodename nor servname provided, or not known curl: (3) [globbing] unmatched close brace/bracket at pos 158
这是文件中的代码
curl -i \ -H '"'Accept: application/json'"' \ -H '"'Content-Type:application/json'"' \ -X POST --data "'"'{"account":{"email":"'$email'","screenName":"'$screenName'","type":"'$theType'","passwordSettings":{"password":"'$password'","passwordConfirm":"'$password'"}},"firstName":"'$firstName'","lastName":"'$lastName'","middleName":"'$middleName'","locale":"'$locale'","registrationSiteId":"'$registrationSiteId'","receiveEmail":"'$receiveEmail'","dateOfBirth":"'$dob'","mobileNumber":"'$mobileNumber'","gender":"'$gender'","fuelActivationDate":"'$fuelActivationDate'","postalCode":"'$postalCode'","country":"'$country'","city":"'$city'","state":"'$state'","bio":"'$bio'","jpFirstNameKana":"'$jpFirstNameKana'","jpLastNameKana":"'$jpLastNameKana'","height":"'$height'","weight":"'$weight'","distanceUnit":"MILES","weightUnit":"POUNDS","heightUnit":"FT/INCHES"}'"'" "https://xxx:xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx"
我认为引号存在问题,但是我经常玩引号,并且遇到了类似的错误。在实际脚本中,所有变量都使用不同的功能定义
您无需传递引号将自定义标头括起来即可卷曲。另外,data应在参数中间加上变量。
data
首先,编写一个函数来生成脚本的发布数据。这使您免于有关shell引用的种种麻烦,并且使读取维护脚本比在curl调用行中输入post数据更容易,如您的尝试:
generate_post_data() { cat <<EOF { "account": { "email": "$email", "screenName": "$screenName", "type": "$theType", "passwordSettings": { "password": "$password", "passwordConfirm": "$password" } }, "firstName": "$firstName", "lastName": "$lastName", "middleName": "$middleName", "locale": "$locale", "registrationSiteId": "$registrationSiteId", "receiveEmail": "$receiveEmail", "dateOfBirth": "$dob", "mobileNumber": "$mobileNumber", "gender": "$gender", "fuelActivationDate": "$fuelActivationDate", "postalCode": "$postalCode", "country": "$country", "city": "$city", "state": "$state", "bio": "$bio", "jpFirstNameKana": "$jpFirstNameKana", "jpLastNameKana": "$jpLastNameKana", "height": "$height", "weight": "$weight", "distanceUnit": "MILES", "weightUnit": "POUNDS", "heightUnit": "FT/INCHES" } EOF }
然后很容易在curl的调用中使用该函数:
curl -i \ -H "Accept: application/json" \ -H "Content-Type:application/json" \ -X POST --data "$(generate_post_data)" "https://xxx:xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx"
就是说,这里有一些关于shell引用规则的说明:
-H参数中的双引号(如所示-H "foo bar")告诉bash将内部内容作为单个参数保留(即使它包含空格)。
-H
-H "foo bar"
--data参数中的单引号(如中的--data 'foo bar')执行相同的操作,只是它们逐字传递所有文本(包括双引号和美元符号)。
--data
--data 'foo bar'
要在单引号引起的文本中间插入变量,您必须结束单引号,然后将其与双引号变量连接,然后重新打开单引号以继续文本:'foo bar'"$variable"'more foo'。
'foo bar'"$variable"'more foo'