我正在尝试向vcloud导演api做cURL。从终端尝试时,这完全正常,但在使用变量替换的bash脚本中尝试时失败。
这是我的脚本无效:
Jul 25 23:11:01 [Genymotion] [Warning] **** STARTING GENYMOTION ****
Jul 25 23:11:01 [Genymotion] [Warning] Genymotion Version: Genymotion "2.7.2"
Jul 25 23:11:01 [Genymotion] [Debug] [LaunchpadApp] Started with ("genymotion")
Jul 25 23:11:01 [Genymotion] [Debug] [doRequest] Requesting: "https://cloud.genymotion.com/launchpad/last_version/linux/x64/"
Jul 25 23:11:01 [Genymotion] [Debug] [doRequest] done
Jul 25 23:11:01 [Genymotion] [Debug] [getGenymotionLastVersion] New version ( "2.7.2" ) available here: "https://www.genymotion.com/download/"
Jul 25 23:11:01 [Genymotion] [Debug] Genymotion is up to date
Jul 25 23:11:01 [Genymotion] [Debug] Loading "vboxmanage" plugin
Jul 25 23:11:01 [Genymotion] [Debug] Plugin "vboxmanage" loaded
Jul 25 23:11:01 [Genymotion] [Debug] Chipset: "GenuineIntel"
Jul 25 23:11:01 [Genymotion] [Debug] CPUID 0x1 (Intel): ECX= "ffba2223"
Jul 25 23:11:01 [Genymotion] [Debug] [VBoxManageCore] Path: "VBoxManage"
Jul 25 23:11:02 [Genymotion] [Debug] VBoxManage ("list", "hostinfo") returns 0
Jul 25 23:11:02 [Genymotion] [Debug] [System properties] Online physical CPU number: 4
Jul 25 23:11:02 [Genymotion] [Debug] [System properties] Online virtual CPU number: 4
Jul 25 23:11:02 [Genymotion] [Debug] CPU number is > 8 - Fix max number to 8
Jul 25 23:11:02 [Genymotion] [Debug] [System properties] Max CPU number: 8
Jul 25 23:11:02 [Genymotion] [Debug] [System properties] Max memory size: 7966
Jul 25 23:11:02 [Genymotion] [Debug] [findHostOnlyInterface] Looking for compatible host-only interface
Jul 25 23:11:02 [Genymotion] [Debug] VBoxManage ("list", "hostonlyifs") returns 0
Jul 25 23:11:02 [Genymotion] [Debug] [createHostOnlyInterface] Creating new host-only interface
Jul 25 23:11:02 [Genymotion] [Error] VBoxManage ("hostonlyif", "create") returns 1
Jul 25 23:11:02 [Genymotion] [Error] Output command: "0%...
Progress state: NS_ERROR_FAILURE
VBoxManage: error: Failed to create the host-only adapter
VBoxManage: error: VBoxNetAdpCtl: Error while adding new interface: VBoxNetAdpCtl: ioctl failed for /dev/vboxnetctl: Inappropriate ioctl for devic
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component HostNetworkInterfaceWrap, interface IHostNetworkInterface
VBoxManage: error: Context: "RTEXITCODE handleCreate(HandlerArg*)" at line 71 of file VBoxManageHostonly.cpp"
Jul 25 23:11:02 [Genymotion] [Error] [createHostOnlyInterface] Failed to create interface
Jul 25 23:11:02 [Genymotion] [Error] "Fail to load vboxmanage plugin from /home/gauravv7/Android/genymotion/genymotion/plugins/"
Jul 25 23:11:02 [Genymotion] [Error] VM Engine failed to load
Jul 25 23:11:02 [Genymotion] [Error] Unable to find VM Engine. Plugin loading aborted.
Jul 25 23:11:02 [Genymotion] [Debug] [LogCollector] VM engine not available
Jul 25 23:11:02 [Genymotion] [Debug] [LogCollector] Creating temporary folder: "/tmp/genymotion-logs-tmp"
以下工作正常:
token=" valid token"
URL="vcdurl"
auth_header=\"x-vcloud-authorization:${token}\"
curl -i -k -H "Accept:application/*+xml;version=1.5" -H "${auth_header}" -X GET $URL/api/vdc/7f252c90-eb3b-43c4-a5ef-458e5bf22c0e > test.txt
echo `cat text.txt`
Error :HTTP/1.1 400 Bad Request
Connection: close
花了几天才弄明白,但我被困在这里。
答案 0 :(得分:0)
token=" valid token"
url="https://vcdurl"
auth_header=x-vcloud-authorization:${token}
curl -i -k -X GET \
-H "Accept:application/*+xml;version=1.5" \
-H "${auth_header}" \
"$url/api/vdc/7f252c90-eb3b-43c4-a5ef-458e5bf22c0e" \
| tee test.txt
请注意:
https://
添加到网址变量的定义中,以使其与手动输入的工作命令保持一致。$URL
的扩展。 (另请注意对小写的更改;请参阅POSIX conventions for environment variable names - 因为shell变量与环境变量共享命名空间,遵守这些约定可避免与对系统或shell有意义的环境变量发生冲突。auth_header
。 (与由shell解析的语法引号不同, literal 引号被视为数据,因此传递给curl
)。echo $(cat test.txt)
消失了。 (顺便说一下,这是一种理智的写作方式cat test.txt
;即使echo "$(cat test.txt)"
会消除最糟糕的不良行为; printf '%s\n' "$(<test.txt)"
会更好,避免需要一个子shell 或一个外部实用程序[因为cat
没有内置到shell本身,因此有调用的开销])。