0

On my gitlab CI I am running the following simple script (.gitlab-ci.yml):

STR=$(cat $FILE)
if grep -q "substring" <<< "$STR"; then echo "ok"; fi

Unfortunatley this gives me the error

/bin/sh: eval: line 100: syntax error: unexpected redirection

Running the same command locally as a script is working as expected:

#!/bin/sh

FILE="./file.txt"
STR=$(cat $FILE)

if grep -q "substring" <<< "$STR"; then
    echo "ok"
fi

The file has the content:

This has a substring somewhere
user3142695
  • 15,844
  • 47
  • 176
  • 332

1 Answers1

1

/bin/sh is not bash and <<< is a bash extension not available on every shell. Install bash, change shebang to /bin/bash and make sure the script is run under bash or use posix compatible syntax printf "%s\n" "$str" | grep...

Note: UPPER CASE VARIABLES are by convention reserved for exported variables, like IFS COLUMNS PWD UID EUID LINES etc. Use lower case variables in your scripts.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Im running this in a alpine image. I think there is no bash but sh. How can I check for substring in sh? – user3142695 Sep 14 '20 at 16:00
  • `or use posix compatible syntax printf "%s\n" "$str" | grep...` https://stackoverflow.com/questions/2829613/how-do-you-tell-if-a-string-contains-another-string-in-posix-sh I meant `if printf "%s\n" "$str" | grep -q somehthing; then`. – KamilCuk Sep 14 '20 at 16:01