I have this shell script
#/bin/bash
SUB_DOMAIN="test.google.com"
IFS=. read -ra __ <<< "${SUB_DOMAIN}"
IFS=. eval 'DOMAIN="${__[*]:(-2)}"'
echo "${DOMAIN} - ${SUB_DOMAIN}"
If I run it under Debian, everything works. But if I start it in a minimal Docker Linux (alpha), I get the following error. Do I have to install anything else there?
line 3: syntax error: unexpected redirection
3
Answers
Most probably you do not have bash installed and you are running your script in posix shell. The here string
<<<
is a bash extension. And bash arrays (and thus expandingarr[*]
) andread -a
are also bash extensions.Yes, actually install bash and then run your script in bash.
Anyway I suggest knowing the environment you are writing for, do not use specific extensions if you want your script to be portable to environments without
bash
and maybe use a more posix friendly (but I think more resource consuming) alternative:And remember that
eval
is evil – a malicious input likeSUB_DOMAIN="test.$(rm -irf /).com"
may cause havoc on thateval
line.Do it with POSIX shell grammar and it should be good everywhere:
You said in a comment:
The sed(1) tool can do this straightforwardly. If you look at the input string as
You can translate this into regular expression syntax:
.
is any single text character,.
is specifically a period,[^.]
is anything but a period,x*
is any number ofx
, and$
is end of string. Parentheses(...)
(in "classic" regexps) mark a group that you’ll want to retrieve later. So you can construct a regexp that matches this string, marking the domain part as a capture group:Then you can use
sed
to replace strings matching this regexp with1
, the contents of the matching group.This will work whenever the POSIX toolset is available, including minimal environments like Alpine containers. It doesn’t depend on any particular shell extensions.