0

I'm looking to understand what is going on in a simple script, that seems to produce random results.

What I am trying to do:

  • Replace variables in pre-existing files from the values defined in the environment.

  • This is done inside a Docker container with a bash script, that runs the command:

    envsubst '$VAR1 $VAR2' < $FILE | tee $FILE


What happens:

  • Sometimes a $FILE in question has contents before the command, but contains nothing after the command.

How to reproduce the issue:

  • Dockerfile:
FROM debian:stretch

RUN apt-get update -qy
RUN apt-get install -qy gettext

COPY main-script /main-script
RUN chmod +x /main-script

ENTRYPOINT [ "/main-script" ]
  • Bash script:
#!/bin/bash

mkdir -p /test

export TEST1=1
export TEST2=2
export TEST3=3

for I in {1..300} ; do
    echo '$TEST1 $TEST2 $TEST3' > /test/file-$I
done

for FILE in /test/file-* ; do
    envsubst < $FILE | tee $FILE
done

for FILE in /test/file-* ; do
    if [[ -z "$(cat $FILE)" ]]; then
        echo "$FILE is empty!"
        FAIL=1
    fi
done

if [[ -n "$FAIL" ]]; then
    exit 2
fi

Output looks something like this:

...
/test/file-11 is empty!
/test/file-180 is empty!
/test/file-183 is empty!
/test/file-295 is empty!
4

1 回答 1

3

管道是异步的,您已经引入了竞争条件。您无法预测之前或之后envsubst的读取是否会截断它。$FILEtee

正确的做法是将更改写入临时文件,然后在成功后将原始文件替换为临时文件。

tmp=$(mktemp)
envsubst < "$FILE" > "$tmp" &&  mv "$tmp" "$FILE"
于 2020-03-05T13:37:41.957 回答