Alpine Linux 中带有转义字符的 if 语句(ash busybox)

发布于 2025-01-13 00:01:12 字数 645 浏览 1 评论 0原文

我有简单的脚本来准备文件,并且它按预期工作。所有代码都在 docker image alpine:latest 执行(所以 ash busybox)

echo "credentials \"url.com\" {token = \"${VALUE}\"}" > ~/.terraformrc

稍后我想准备测试,我正在使用 if [[ ]] 语法。

if [[ $(cat ~/.terraformrc) != "credentials \"url.com\" {token =\"VALUE\" }" ]]; then exit 1; fi

在 Bash 测试有效,但在 ash/busybox/alpine 上,

sh: "url.com": unknown operand

当我 echo all 时,我也会在 ash/busybox/alpine 上正确转义

echo "credentials \"url.com\" {token = \"VALUE\" }"
credentials "url.com" {token = "VALUE" }

我应该如何在 [ ] 或 [[ ]] 内转义 " ? 谢谢帮助

I have simple script to prepare file and it is working as intended. All code is executed at docker image alpine:latest (so ash busybox)

echo "credentials \"url.com\" {token = \"${VALUE}\"}" > ~/.terraformrc

Later I want to prepare test and I am using if [[ ]] syntax for that.

if [[ $(cat ~/.terraformrc) != "credentials \"url.com\" {token =\"VALUE\" }" ]]; then exit 1; fi

On Bash test is working but on ash/busybox/alpine I get

sh: "url.com": unknown operand

Also on ash/busybox/alpine when I do echo all is escaping correctly

echo "credentials \"url.com\" {token = \"VALUE\" }"
credentials "url.com" {token = "VALUE" }

How should I escape " inside [ ] or [[ ]] ?
Thx for help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2025-01-20 00:01:12

[[ ]] 用于 bash,对于 ash 你应该使用 POSIX [ ] 代替:

#!/bin/sh

value=VALUE001

echo "credentials \"url.com\" {token = \"$value\"}" > ~/.terraformrc

if [ "$(cat ~/.terraformrc)" != "credentials \"url.com\" {token =\"VALUE001\" }" ]
then
    exit 1
fi

顺便说一句,你正确地进行了转义,但你忘记了双引号 $ (cat ~/.terraformrc)

[[ ]] is for bash, for ash you should use POSIX [ ] instead:

#!/bin/sh

value=VALUE001

echo "credentials \"url.com\" {token = \"$value\"}" > ~/.terraformrc

if [ "$(cat ~/.terraformrc)" != "credentials \"url.com\" {token =\"VALUE001\" }" ]
then
    exit 1
fi

BTW, you were doing the escaping correctly but you forgot to double quote $(cat ~/.terraformrc)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文