首页 > 解决方案 > 使用包含特殊字符的变量的 shell 脚本

问题描述

我在 shell 脚本中使用 Redis,我正在尝试设置一个键的值

这是我的脚本可以正常工作:

:/# redis-cli JSON.SET etat . '{"name":"Eric"}'
Ok

但是当我使用包含我的 Json 的变量时,我得到了:

:/# json="'{\"name\":\"Erci\"}'"
:/# ehco $json
'{"name":"Eric"}'
:/# redis-cli JSON.SET etat . $json
(error) ERR wrong number of arguments for 'JSON.SET' command

我试过了 :

:/# redis-cli JSON.SET etat . ${json}
(error) ERR wrong number of arguments for 'JSON.SET' command

和:

:/# redis-cli JSON.SET etat . "$json"
(error) ERR JSON lexer error SPECIAL_EXPECTED at position 26

有人可以帮忙吗?

标签: shellredisspecial-characters

解决方案


您不需要变量内的单个刻度:

$ json="{\"name\":\"Erci\"}"
$ redis-cli JSON.SET etat . "$json"
OK
$ redis-cli JSON.GET etat
"{\"name\":\"Erci\"}"

推荐阅读