一些背景:
我正在尝试将命令作为 bash 脚本的一部分运行以轮询系统/设备数据。
# Recommended usage in the command line, contains single and double quotes
wbemcli ein 'http://localhost:5988/root/cimv2:some_class'
wbemcli gi 'http://localhost:5988/root/cimv2:some_class.DeviceID="Some Device ID"'
# I can get away with just 1 level of quotation marks
wbemcli ein http://localhost:5988/root/cimv2:some_class
wbemcli gi http://localhost:5988/root/cimv2:some_class.DeviceID="Some Device ID"
wbemcli gi http://localhost:5988/root/cimv2:some_class.DeviceID='Some Device ID'
所以这是有效的......
#!/bin/sh
C_PATH=http://localhost:5988/root/cimv2
CLASS=some_class
ID="Some Device ID"
set -x
wbemcli ein $C_PATH:$CLASS
不幸的是,当我尝试将引号集成到命令中时,它就崩溃了。在 shell 中执行的代码出乎我的意料。
# Code
wbemcli gi $C_PATH:$CLASS.DeviceID=\"$ID\"
output $ ./myscript
+ wbemcli gi 'http://localhost:5988/root/cimv2:some_class.DeviceID="Some' Device 'ID"'
# I was expecting this ...
output $ ./myscript
+ wbemcli gi http://localhost:5988/root/cimv2:some_class.DeviceID="Some Device ID"
Bash 在我没想到的地方添加了引号。它甚至将整个 URL 部分括在单引号中。这是怎么回事 ?