Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash multiple heredocs et al

I have a bash build script that I (well, Jenkins, but that's immaterial, this is a bash question) executes like this:

sudo -u buildmaster bash <<'END'
# do all sorts of crazy stuff here including using variables, like:
ARGS=something
ARGS="$ARGS or other"
# and so forth
END

Now I'd like to pass in a variable (Jenkins parameterized build), say PLATFORM. The trouble is that if I refer to $PLATFORM in my heredoc, it is undefined (I used 'END' to avoid variable substitution). If I turn the 'END' into END, my scripts becomes really unreadable with all the escapes I need to use.

So the question is, is there some (easy, readable) way of passing in two heredocs, one with quotes, and one without, into the same bash invocation? I was looking for something like this

sudo -u buildmaster bash <<PREFIX <<'END'
PLATFORM=$PLATFORM
PREFIX
# previous heredoc goes here
END

in the hope that it would simply concatenate, but I can't get the two heredocs to work (I guess bash is not Perl)

My fallback plan is to create temp files, but I was hoping there's a trick I don't know and that somebody could teach me :-)

like image 652
Johannes Ernst Avatar asked May 14 '26 08:05

Johannes Ernst


1 Answers

In this case you could consider using bash with its -s argument:

sudo -u buildmaster bash -s -- "$PARAMETER" <<'END'
# do all sorts of crazy stuff here including using variables, like:
ARGS=something
ARGS="$ARGS or other"
# and so forth
PARAMETER=$1
END

(note that you have a syntax error, your closing END is quoted and shouldn't be).


There's another possibility (so that you have lots of options—and this one might be the simplest, if it applies) is to have your variables exported, and use the -E switch to sudo that exports the environment, e.g.,

export PARAMETER
sudo -E -u buildmaster bash <<'EOF'
# do all sorts of crazy stuff here including using variables, like:
ARGS=something
ARGS="$ARGS or other"
# and so forth
# you can use PARAMETER here, it's fine!
EOF

Along these lines, if you don't want to export everything, you may use env as follows:

sudo -u buildmaster env PARAMETER="$PARAMETER" bash <<'EOF'
# do all sorts of crazy stuff here including using variables, like:
ARGS=something
ARGS="$ARGS or other"
# and so forth
# you can use PARAMETER here, it's fine!
EOF
like image 56
gniourf_gniourf Avatar answered May 17 '26 01:05

gniourf_gniourf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!