shell - 使用包含特殊字符的变量的 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
有人可以帮忙吗?
解决方案
您不需要变量内的单个刻度:
$ json="{\"name\":\"Erci\"}"
$ redis-cli JSON.SET etat . "$json"
OK
$ redis-cli JSON.GET etat
"{\"name\":\"Erci\"}"
推荐阅读
- java - 嵌套for循环输出打印和格式化不正确的表格,这一切都很时髦
- python - 在使用python和mysql的时候,并不是所有的参数都用在SQL语句中
- json - .net 中的 curl 命令
- scala - 在运行时访问 Play Framework Router 路由表
- java - 仅通过 Google Java 库上的操作登录 Google 屏幕?
- qt - 如何让 QT creator 构建的应用程序在 macOS Mojave 上始终使用轻模式主题?
- javascript - 通过 For 循环创建 HTML 不能完全正常工作
- javascript - 为什么 Map 原型没有 `.map()` 函数?
- rust - FromStr 特征
- javascript - 在每个 forEach 语句之间添加等待