所以,我在我的bash脚本中处理了几个函数。我有一个有效,但我的第二个包括用双引号""
括起来的一些值,从而打破了我的变量替换。我试过用一个刻度来围绕这些值,但在某些情况下这并不起作用。
例如:
# Functions
unset_versions ()
{
host='127.0.0.1'
db='mydev'
_account='"foo"'
_profile='"bar"'
_mongo=$(which mongo);
exp="db.profile_versions_20170420.update({account:${_account}, profile:${_profile}, version:${_version}},{ \$unset : { labels : true }});";
${_mongo} ${host}/${db} --eval "$exp"
}
然而,这不会起作用:
update_versions ()
{
host='127.0.0.1'
db='mydev'
_account='"foo"'
_profile='"bar"'
_mongo=$(which mongo);
exp="db.profile_versions_20170420.update({account:${_account}, profile:${_profile}, version:${_version}},{ \$set : { labels : { "1" : { "name" : "data layer patch", "color" : "regal-rose" }, "2" : { "name" : "Global Functions", "color" : "melon-mambo" }}}});
${_mongo} ${host}/${db} --eval "$exp"
}
我知道这看起来很难看。如果有更好的方法,请提供一些建议。我也尝试过做exp=$(....)
,但这似乎也不起作用。
答案 0 :(得分:1)
你应该像这样逃避'“':
exp="db.profile_versions_20170420.update({account:${_account},profile:${_profile}, version:${_version}},{ \$set : { labels : { \"1\" : { \"name\" : "datalayer patch\", \"color\" : \"regal-rose\" }, \"2\" : { \"name\" : "Global Functions\", \"color\" : \"melon-mambo\" }}}})";