简体   繁体   中英

If statements with escape characters in Alpine Linux (ash busybox)

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

[[ ]] 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM