Adasiek
Adasiek

Reputation: 65

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

Upvotes: 1

Views: 1218

Answers (1)

Fravadona
Fravadona

Reputation: 17208

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

Upvotes: 4

Related Questions