Last active
February 19, 2023 17:21
-
-
Save ferki/1ba37363e88344b290f41b5b2f66aa5d to your computer and use it in GitHub Desktop.
Rex run with HEREDOC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.26; | |
use Rex; | |
task 'heredoc_run', sub { | |
run 'foo', command => <<~'EOF', | |
f=$(cat /tmp/num) | |
if [ "$f" == 2 ]; then | |
ls -l / >/tmp/foo.txt | |
else | |
ls -l / >/tmp/bar.txt | |
fi | |
EOF | |
unless => <<~'EOF'; | |
f=$(cat /tmp/num) | |
if [ "$f" == 2 ]; then | |
exit 1 | |
else | |
exit 0 | |
fi | |
EOF | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, that should work too! 👍
run
takes arguments as strings, so these could he HEREDOCs as well.It's also possible to add single quotes in the the HEREDOC starter indicator, like
<<'EOF'
and then the whole HEREDOC will be treated as a string within single quotes (and this less escaping have to be done e.g. every\$
can become the more naturally readable$
).With adding
v5.26
, indented HEREDOCs work nicely as well with<<~'EOF'
if needed. Then the whole HEREDOC and the end marker string can be indented too, leading to even more readable code.