Conditionally dry run a command block with BASH heredoc
I wanted to have something as simple as that in my script:
set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}
[ "$1" == "-dryrun" ] && set_dryrun
${BEGIN}
ls -l
${END}
So that if I run it with the -dryrun parameter, commands between ${BEGIN} and ${END} would be just printed, but not executed.
Everything goes well if I run without the -dryrun flag:
$ ./dryrun_opt
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:15 dryrun_opt
With -dryrun, though, I see:
$ ./dryrun_opt -dryrun
cat: '<<': No such file or directory
cat: EOF: No such file or directory
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:16 dryrun_opt
./dryrun_opt: line 14: EOF: command not found
Well... I am not replacing (expanding?) my heredoc correctly, I know, but how should I, if possible?
Thanks in advance.
bash bash-scripting debug
add a comment |
I wanted to have something as simple as that in my script:
set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}
[ "$1" == "-dryrun" ] && set_dryrun
${BEGIN}
ls -l
${END}
So that if I run it with the -dryrun parameter, commands between ${BEGIN} and ${END} would be just printed, but not executed.
Everything goes well if I run without the -dryrun flag:
$ ./dryrun_opt
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:15 dryrun_opt
With -dryrun, though, I see:
$ ./dryrun_opt -dryrun
cat: '<<': No such file or directory
cat: EOF: No such file or directory
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:16 dryrun_opt
./dryrun_opt: line 14: EOF: command not found
Well... I am not replacing (expanding?) my heredoc correctly, I know, but how should I, if possible?
Thanks in advance.
bash bash-scripting debug
add a comment |
I wanted to have something as simple as that in my script:
set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}
[ "$1" == "-dryrun" ] && set_dryrun
${BEGIN}
ls -l
${END}
So that if I run it with the -dryrun parameter, commands between ${BEGIN} and ${END} would be just printed, but not executed.
Everything goes well if I run without the -dryrun flag:
$ ./dryrun_opt
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:15 dryrun_opt
With -dryrun, though, I see:
$ ./dryrun_opt -dryrun
cat: '<<': No such file or directory
cat: EOF: No such file or directory
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:16 dryrun_opt
./dryrun_opt: line 14: EOF: command not found
Well... I am not replacing (expanding?) my heredoc correctly, I know, but how should I, if possible?
Thanks in advance.
bash bash-scripting debug
I wanted to have something as simple as that in my script:
set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}
[ "$1" == "-dryrun" ] && set_dryrun
${BEGIN}
ls -l
${END}
So that if I run it with the -dryrun parameter, commands between ${BEGIN} and ${END} would be just printed, but not executed.
Everything goes well if I run without the -dryrun flag:
$ ./dryrun_opt
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:15 dryrun_opt
With -dryrun, though, I see:
$ ./dryrun_opt -dryrun
cat: '<<': No such file or directory
cat: EOF: No such file or directory
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:16 dryrun_opt
./dryrun_opt: line 14: EOF: command not found
Well... I am not replacing (expanding?) my heredoc correctly, I know, but how should I, if possible?
Thanks in advance.
bash bash-scripting debug
bash bash-scripting debug
asked Jan 25 at 16:20
j4xj4x
1175
1175
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This works for me:
set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}
[ "$1" == "-dryrun" ] && set_dryrun
eval "${BEGIN}
ls -l
${END}"
However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.
Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:
if [ "$1" == "-dryrun" ]
then
CMD="cat"
else
CMD="bash" # your preferred shell here
fi
WORK="$(cat <<'EOF'
# abc'asdf"
# $(dont-execute-this)
# foo"bar"''
ls -l
echo "$PATH"
echo "$PATH"
EOF
)"
$CMD <<< "$WORK"
Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.
– j4x
Jan 28 at 6:42
add a comment |
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398453%2fconditionally-dry-run-a-command-block-with-bash-heredoc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This works for me:
set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}
[ "$1" == "-dryrun" ] && set_dryrun
eval "${BEGIN}
ls -l
${END}"
However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.
Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:
if [ "$1" == "-dryrun" ]
then
CMD="cat"
else
CMD="bash" # your preferred shell here
fi
WORK="$(cat <<'EOF'
# abc'asdf"
# $(dont-execute-this)
# foo"bar"''
ls -l
echo "$PATH"
echo "$PATH"
EOF
)"
$CMD <<< "$WORK"
Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.
– j4x
Jan 28 at 6:42
add a comment |
This works for me:
set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}
[ "$1" == "-dryrun" ] && set_dryrun
eval "${BEGIN}
ls -l
${END}"
However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.
Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:
if [ "$1" == "-dryrun" ]
then
CMD="cat"
else
CMD="bash" # your preferred shell here
fi
WORK="$(cat <<'EOF'
# abc'asdf"
# $(dont-execute-this)
# foo"bar"''
ls -l
echo "$PATH"
echo "$PATH"
EOF
)"
$CMD <<< "$WORK"
Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.
– j4x
Jan 28 at 6:42
add a comment |
This works for me:
set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}
[ "$1" == "-dryrun" ] && set_dryrun
eval "${BEGIN}
ls -l
${END}"
However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.
Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:
if [ "$1" == "-dryrun" ]
then
CMD="cat"
else
CMD="bash" # your preferred shell here
fi
WORK="$(cat <<'EOF'
# abc'asdf"
# $(dont-execute-this)
# foo"bar"''
ls -l
echo "$PATH"
echo "$PATH"
EOF
)"
$CMD <<< "$WORK"
This works for me:
set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}
[ "$1" == "-dryrun" ] && set_dryrun
eval "${BEGIN}
ls -l
${END}"
However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.
Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:
if [ "$1" == "-dryrun" ]
then
CMD="cat"
else
CMD="bash" # your preferred shell here
fi
WORK="$(cat <<'EOF'
# abc'asdf"
# $(dont-execute-this)
# foo"bar"''
ls -l
echo "$PATH"
echo "$PATH"
EOF
)"
$CMD <<< "$WORK"
edited Jan 25 at 18:20
answered Jan 25 at 17:46
Jim L.Jim L.
44117
44117
Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.
– j4x
Jan 28 at 6:42
add a comment |
Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.
– j4x
Jan 28 at 6:42
Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.
– j4x
Jan 28 at 6:42
Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.
– j4x
Jan 28 at 6:42
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398453%2fconditionally-dry-run-a-command-block-with-bash-heredoc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
