1

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

Adasiek
  • 65
  • 1
  • 7
  • In ash `[[` will not work. And what is `-` doing there in front? Please specify the context. If you are running jenkins, gitlab-ci, github-actions, please specify. – KamilCuk Mar 08 '22 at 14:10
  • I am running this on gitlab-ci on alpine:latest. – Adasiek Mar 08 '22 at 14:13

1 Answers1

3

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

Fravadona
  • 13,917
  • 1
  • 23
  • 35