What does shellcheck warning SC2129 “Consider using cmd1; cmd2; >> file instead of individual redirects.” mean?How to create (via installer script) a task that will install my bash script so it runs on DE startup?Shell script & “nvidia-smi” - needs right command/flag!Can line draw characters (or colors) be added to a Bash file list menu?Cannot run script (is executable, hash bang matches shell)Script works manually, but not when called by Cron
what's the difference between these two expressions: "for good" and "for keep"
Has there been a multiethnic Star Trek character?
How can I get an unreasonable manager to approve time off?
Fermat's statement about the ancients: How serious was he?
Getting UPS Power from One Room to Another
How to safely destroy (a large quantity of) valid checks?
Determining fair price for profitable mobile app business
Why are MBA programs closing?
How to ensure color fidelity of the same file on two computers?
How to communicate to my GM that not being allowed to use stealth isn't fun for me?
Traversing Oceania: A Cryptic Journey
Is it possible for a vehicle to be manufactured without a catalytic converter?
CSV how to trim values to 2 places in multiple columns using UNIX
LuaLaTex - how to use number, computed later in the document
Why does the Mishnah use the terms poor person and homeowner when discussing carrying on Shabbat?
How to decline a wedding invitation from a friend I haven't seen in years?
Meaning of 'lose their grip on the groins of their followers'
How is the excise border managed in Ireland?
Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?
Does the Long March-11 increase its thrust after clearing the launch tower?
Entire circuit dead after GFCI outlet
What ways have you found to get edits from non-LaTeX users?
HR woman suggesting me I should not hang out with the coworker
Are polynomials with the same roots identical?
What does shellcheck warning SC2129 “Consider using cmd1; cmd2; >> file instead of individual redirects.” mean?
How to create (via installer script) a task that will install my bash script so it runs on DE startup?Shell script & “nvidia-smi” - needs right command/flag!Can line draw characters (or colors) be added to a Bash file list menu?Cannot run script (is executable, hash bang matches shell)Script works manually, but not when called by Cron
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have this shellcheck
warning I can't figure out:
In /mnt/e/bin/iconic line 540:
printf "FALSE|" >> "$IconsRaw" # Select field number 1
^-- SC2129: Consider using cmd1; cmd2; >> file instead of individual redirects.
I've noticed many of us here use shellcheck to fix our bash scripts / shell commands so I hope the question is on topic.
As per comments posting relevant section of bash script:
if [[ "$X" == "?" || "$Y" == "?" ]] ; then
: # Bad X or Y offset usually "Link to Name.ext~" (backup name)
else
let i++
printf "FALSE|" >> "$IconsRaw" # Select field number 1
printf "%s|" "$i" >> "$IconsRaw" # 2
printf "%s|" "$File##*/" >> "$IconsRaw"
printf "%s|" "$Linkless" >> "$IconsRaw" # 4
printf "%s|" "$Date" >> "$IconsRaw" # 5
printf "%s|" "$X" >> "$IconsRaw" # 6
echo "$Y" >> "$IconsRaw" # 7
fi
Solution
Thanks to accepted answer and comments I've learned that shellcheck
not only catches errors in your code, but also suggests performance improvements. In this case the filename $IconsRaw
was being opened and closed many times with each printf
and echo
.
The more efficient bash code:
# X,Y screen coordinates invalid on backup files ending with "~"
! [[ "$X" == "?" || "$Y" == "?" ]] && let i++; echo
"FALSE
bash debugging output shellcheck
add a comment |
I have this shellcheck
warning I can't figure out:
In /mnt/e/bin/iconic line 540:
printf "FALSE|" >> "$IconsRaw" # Select field number 1
^-- SC2129: Consider using cmd1; cmd2; >> file instead of individual redirects.
I've noticed many of us here use shellcheck to fix our bash scripts / shell commands so I hope the question is on topic.
As per comments posting relevant section of bash script:
if [[ "$X" == "?" || "$Y" == "?" ]] ; then
: # Bad X or Y offset usually "Link to Name.ext~" (backup name)
else
let i++
printf "FALSE|" >> "$IconsRaw" # Select field number 1
printf "%s|" "$i" >> "$IconsRaw" # 2
printf "%s|" "$File##*/" >> "$IconsRaw"
printf "%s|" "$Linkless" >> "$IconsRaw" # 4
printf "%s|" "$Date" >> "$IconsRaw" # 5
printf "%s|" "$X" >> "$IconsRaw" # 6
echo "$Y" >> "$IconsRaw" # 7
fi
Solution
Thanks to accepted answer and comments I've learned that shellcheck
not only catches errors in your code, but also suggests performance improvements. In this case the filename $IconsRaw
was being opened and closed many times with each printf
and echo
.
The more efficient bash code:
# X,Y screen coordinates invalid on backup files ending with "~"
! [[ "$X" == "?" || "$Y" == "?" ]] && let i++; echo
"FALSE
bash debugging output shellcheck
1
I think it's just suggesting doing a single write instead of multiple open/seek/write operations - but it might be clearer if you posted a larger sample of the script
– steeldriver
Jun 1 at 1:46
2
When the message is too terse, you can look at the wiki page for SC2129 which has a full length description with examples
– that other guy
Jun 1 at 8:09
@thatotherguy Your comment could be an answer if it included relevant parts of the link in the body. It would help others and I for one would gladly upvote it.
– WinEunuuchs2Unix
Jun 1 at 15:07
add a comment |
I have this shellcheck
warning I can't figure out:
In /mnt/e/bin/iconic line 540:
printf "FALSE|" >> "$IconsRaw" # Select field number 1
^-- SC2129: Consider using cmd1; cmd2; >> file instead of individual redirects.
I've noticed many of us here use shellcheck to fix our bash scripts / shell commands so I hope the question is on topic.
As per comments posting relevant section of bash script:
if [[ "$X" == "?" || "$Y" == "?" ]] ; then
: # Bad X or Y offset usually "Link to Name.ext~" (backup name)
else
let i++
printf "FALSE|" >> "$IconsRaw" # Select field number 1
printf "%s|" "$i" >> "$IconsRaw" # 2
printf "%s|" "$File##*/" >> "$IconsRaw"
printf "%s|" "$Linkless" >> "$IconsRaw" # 4
printf "%s|" "$Date" >> "$IconsRaw" # 5
printf "%s|" "$X" >> "$IconsRaw" # 6
echo "$Y" >> "$IconsRaw" # 7
fi
Solution
Thanks to accepted answer and comments I've learned that shellcheck
not only catches errors in your code, but also suggests performance improvements. In this case the filename $IconsRaw
was being opened and closed many times with each printf
and echo
.
The more efficient bash code:
# X,Y screen coordinates invalid on backup files ending with "~"
! [[ "$X" == "?" || "$Y" == "?" ]] && let i++; echo
"FALSE
bash debugging output shellcheck
I have this shellcheck
warning I can't figure out:
In /mnt/e/bin/iconic line 540:
printf "FALSE|" >> "$IconsRaw" # Select field number 1
^-- SC2129: Consider using cmd1; cmd2; >> file instead of individual redirects.
I've noticed many of us here use shellcheck to fix our bash scripts / shell commands so I hope the question is on topic.
As per comments posting relevant section of bash script:
if [[ "$X" == "?" || "$Y" == "?" ]] ; then
: # Bad X or Y offset usually "Link to Name.ext~" (backup name)
else
let i++
printf "FALSE|" >> "$IconsRaw" # Select field number 1
printf "%s|" "$i" >> "$IconsRaw" # 2
printf "%s|" "$File##*/" >> "$IconsRaw"
printf "%s|" "$Linkless" >> "$IconsRaw" # 4
printf "%s|" "$Date" >> "$IconsRaw" # 5
printf "%s|" "$X" >> "$IconsRaw" # 6
echo "$Y" >> "$IconsRaw" # 7
fi
Solution
Thanks to accepted answer and comments I've learned that shellcheck
not only catches errors in your code, but also suggests performance improvements. In this case the filename $IconsRaw
was being opened and closed many times with each printf
and echo
.
The more efficient bash code:
# X,Y screen coordinates invalid on backup files ending with "~"
! [[ "$X" == "?" || "$Y" == "?" ]] && let i++; echo
"FALSE
bash debugging output shellcheck
bash debugging output shellcheck
edited Jun 1 at 16:20
WinEunuuchs2Unix
asked Jun 1 at 1:43
WinEunuuchs2UnixWinEunuuchs2Unix
51k13100199
51k13100199
1
I think it's just suggesting doing a single write instead of multiple open/seek/write operations - but it might be clearer if you posted a larger sample of the script
– steeldriver
Jun 1 at 1:46
2
When the message is too terse, you can look at the wiki page for SC2129 which has a full length description with examples
– that other guy
Jun 1 at 8:09
@thatotherguy Your comment could be an answer if it included relevant parts of the link in the body. It would help others and I for one would gladly upvote it.
– WinEunuuchs2Unix
Jun 1 at 15:07
add a comment |
1
I think it's just suggesting doing a single write instead of multiple open/seek/write operations - but it might be clearer if you posted a larger sample of the script
– steeldriver
Jun 1 at 1:46
2
When the message is too terse, you can look at the wiki page for SC2129 which has a full length description with examples
– that other guy
Jun 1 at 8:09
@thatotherguy Your comment could be an answer if it included relevant parts of the link in the body. It would help others and I for one would gladly upvote it.
– WinEunuuchs2Unix
Jun 1 at 15:07
1
1
I think it's just suggesting doing a single write instead of multiple open/seek/write operations - but it might be clearer if you posted a larger sample of the script
– steeldriver
Jun 1 at 1:46
I think it's just suggesting doing a single write instead of multiple open/seek/write operations - but it might be clearer if you posted a larger sample of the script
– steeldriver
Jun 1 at 1:46
2
2
When the message is too terse, you can look at the wiki page for SC2129 which has a full length description with examples
– that other guy
Jun 1 at 8:09
When the message is too terse, you can look at the wiki page for SC2129 which has a full length description with examples
– that other guy
Jun 1 at 8:09
@thatotherguy Your comment could be an answer if it included relevant parts of the link in the body. It would help others and I for one would gladly upvote it.
– WinEunuuchs2Unix
Jun 1 at 15:07
@thatotherguy Your comment could be an answer if it included relevant parts of the link in the body. It would help others and I for one would gladly upvote it.
– WinEunuuchs2Unix
Jun 1 at 15:07
add a comment |
1 Answer
1
active
oldest
votes
I assume your script has multiple instances of >> "$IconsRaw"
. That message is suggesting redirecting the output only once and grouping the commands in a subshell. Presumably to avoid the overhead of opening and closing the file multiple times.
So, instead of this:
printf "FALSE|" >> "$IconsRaw" # Select field number 1
printf "%s|" "$i" >> "$IconsRaw" # 2
printf "%s|" "$File##*/" >> "$IconsRaw"
printf "%s|" "$Linkless" >> "$IconsRaw" # 4
printf "%s|" "$Date" >> "$IconsRaw" # 5
printf "%s|" "$X" >> "$IconsRaw" # 6
echo "$Y" >> "$IconsRaw" # 7
This:
" "$File##*/"
printf "%s >> "$IconsRaw"
Bu that's also a needless repetition of printf
and it is more efficient to just do:
printf '%s|%s|%s|%s|%s|%s|%sn'
'FALSE' "$i" "$File##*/" "$Linkless"
"$Date" "$X" "$Y" >> "$IconsRaw"
1
printf also can write all arguments using only one format specifier provided. So you could have one printf and multiple args to it as alternative. Or use a function. Functions support redirection in declaration
– Sergiy Kolodyazhnyy
Jun 1 at 3:11
@WinEunuuchs2Unix please see updated answer. There's no need for multiple calls toprintf
at all and if you only wantn
on the last field, then just only usen
for the last field.
– terdon♦
Jun 1 at 12:11
@SergiyKolodyazhnyy excellent point of concatenating all fields into singleprintf
call. By the same token a singleecho
could be used with shorter code (no%s
) but I wonder if that is less efficient? Terdon thanks for your answer. Accepting now.
– WinEunuuchs2Unix
Jun 1 at 15:11
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
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%2faskubuntu.com%2fquestions%2f1147775%2fwhat-does-shellcheck-warning-sc2129-consider-using-cmd1-cmd2-file-inst%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
I assume your script has multiple instances of >> "$IconsRaw"
. That message is suggesting redirecting the output only once and grouping the commands in a subshell. Presumably to avoid the overhead of opening and closing the file multiple times.
So, instead of this:
printf "FALSE|" >> "$IconsRaw" # Select field number 1
printf "%s|" "$i" >> "$IconsRaw" # 2
printf "%s|" "$File##*/" >> "$IconsRaw"
printf "%s|" "$Linkless" >> "$IconsRaw" # 4
printf "%s|" "$Date" >> "$IconsRaw" # 5
printf "%s|" "$X" >> "$IconsRaw" # 6
echo "$Y" >> "$IconsRaw" # 7
This:
" "$File##*/"
printf "%s >> "$IconsRaw"
Bu that's also a needless repetition of printf
and it is more efficient to just do:
printf '%s|%s|%s|%s|%s|%s|%sn'
'FALSE' "$i" "$File##*/" "$Linkless"
"$Date" "$X" "$Y" >> "$IconsRaw"
1
printf also can write all arguments using only one format specifier provided. So you could have one printf and multiple args to it as alternative. Or use a function. Functions support redirection in declaration
– Sergiy Kolodyazhnyy
Jun 1 at 3:11
@WinEunuuchs2Unix please see updated answer. There's no need for multiple calls toprintf
at all and if you only wantn
on the last field, then just only usen
for the last field.
– terdon♦
Jun 1 at 12:11
@SergiyKolodyazhnyy excellent point of concatenating all fields into singleprintf
call. By the same token a singleecho
could be used with shorter code (no%s
) but I wonder if that is less efficient? Terdon thanks for your answer. Accepting now.
– WinEunuuchs2Unix
Jun 1 at 15:11
add a comment |
I assume your script has multiple instances of >> "$IconsRaw"
. That message is suggesting redirecting the output only once and grouping the commands in a subshell. Presumably to avoid the overhead of opening and closing the file multiple times.
So, instead of this:
printf "FALSE|" >> "$IconsRaw" # Select field number 1
printf "%s|" "$i" >> "$IconsRaw" # 2
printf "%s|" "$File##*/" >> "$IconsRaw"
printf "%s|" "$Linkless" >> "$IconsRaw" # 4
printf "%s|" "$Date" >> "$IconsRaw" # 5
printf "%s|" "$X" >> "$IconsRaw" # 6
echo "$Y" >> "$IconsRaw" # 7
This:
" "$File##*/"
printf "%s >> "$IconsRaw"
Bu that's also a needless repetition of printf
and it is more efficient to just do:
printf '%s|%s|%s|%s|%s|%s|%sn'
'FALSE' "$i" "$File##*/" "$Linkless"
"$Date" "$X" "$Y" >> "$IconsRaw"
1
printf also can write all arguments using only one format specifier provided. So you could have one printf and multiple args to it as alternative. Or use a function. Functions support redirection in declaration
– Sergiy Kolodyazhnyy
Jun 1 at 3:11
@WinEunuuchs2Unix please see updated answer. There's no need for multiple calls toprintf
at all and if you only wantn
on the last field, then just only usen
for the last field.
– terdon♦
Jun 1 at 12:11
@SergiyKolodyazhnyy excellent point of concatenating all fields into singleprintf
call. By the same token a singleecho
could be used with shorter code (no%s
) but I wonder if that is less efficient? Terdon thanks for your answer. Accepting now.
– WinEunuuchs2Unix
Jun 1 at 15:11
add a comment |
I assume your script has multiple instances of >> "$IconsRaw"
. That message is suggesting redirecting the output only once and grouping the commands in a subshell. Presumably to avoid the overhead of opening and closing the file multiple times.
So, instead of this:
printf "FALSE|" >> "$IconsRaw" # Select field number 1
printf "%s|" "$i" >> "$IconsRaw" # 2
printf "%s|" "$File##*/" >> "$IconsRaw"
printf "%s|" "$Linkless" >> "$IconsRaw" # 4
printf "%s|" "$Date" >> "$IconsRaw" # 5
printf "%s|" "$X" >> "$IconsRaw" # 6
echo "$Y" >> "$IconsRaw" # 7
This:
" "$File##*/"
printf "%s >> "$IconsRaw"
Bu that's also a needless repetition of printf
and it is more efficient to just do:
printf '%s|%s|%s|%s|%s|%s|%sn'
'FALSE' "$i" "$File##*/" "$Linkless"
"$Date" "$X" "$Y" >> "$IconsRaw"
I assume your script has multiple instances of >> "$IconsRaw"
. That message is suggesting redirecting the output only once and grouping the commands in a subshell. Presumably to avoid the overhead of opening and closing the file multiple times.
So, instead of this:
printf "FALSE|" >> "$IconsRaw" # Select field number 1
printf "%s|" "$i" >> "$IconsRaw" # 2
printf "%s|" "$File##*/" >> "$IconsRaw"
printf "%s|" "$Linkless" >> "$IconsRaw" # 4
printf "%s|" "$Date" >> "$IconsRaw" # 5
printf "%s|" "$X" >> "$IconsRaw" # 6
echo "$Y" >> "$IconsRaw" # 7
This:
" "$File##*/"
printf "%s >> "$IconsRaw"
Bu that's also a needless repetition of printf
and it is more efficient to just do:
printf '%s|%s|%s|%s|%s|%s|%sn'
'FALSE' "$i" "$File##*/" "$Linkless"
"$Date" "$X" "$Y" >> "$IconsRaw"
edited Jun 1 at 12:10
answered Jun 1 at 1:47
terdon♦terdon
70.2k13146230
70.2k13146230
1
printf also can write all arguments using only one format specifier provided. So you could have one printf and multiple args to it as alternative. Or use a function. Functions support redirection in declaration
– Sergiy Kolodyazhnyy
Jun 1 at 3:11
@WinEunuuchs2Unix please see updated answer. There's no need for multiple calls toprintf
at all and if you only wantn
on the last field, then just only usen
for the last field.
– terdon♦
Jun 1 at 12:11
@SergiyKolodyazhnyy excellent point of concatenating all fields into singleprintf
call. By the same token a singleecho
could be used with shorter code (no%s
) but I wonder if that is less efficient? Terdon thanks for your answer. Accepting now.
– WinEunuuchs2Unix
Jun 1 at 15:11
add a comment |
1
printf also can write all arguments using only one format specifier provided. So you could have one printf and multiple args to it as alternative. Or use a function. Functions support redirection in declaration
– Sergiy Kolodyazhnyy
Jun 1 at 3:11
@WinEunuuchs2Unix please see updated answer. There's no need for multiple calls toprintf
at all and if you only wantn
on the last field, then just only usen
for the last field.
– terdon♦
Jun 1 at 12:11
@SergiyKolodyazhnyy excellent point of concatenating all fields into singleprintf
call. By the same token a singleecho
could be used with shorter code (no%s
) but I wonder if that is less efficient? Terdon thanks for your answer. Accepting now.
– WinEunuuchs2Unix
Jun 1 at 15:11
1
1
printf also can write all arguments using only one format specifier provided. So you could have one printf and multiple args to it as alternative. Or use a function. Functions support redirection in declaration
– Sergiy Kolodyazhnyy
Jun 1 at 3:11
printf also can write all arguments using only one format specifier provided. So you could have one printf and multiple args to it as alternative. Or use a function. Functions support redirection in declaration
– Sergiy Kolodyazhnyy
Jun 1 at 3:11
@WinEunuuchs2Unix please see updated answer. There's no need for multiple calls to
printf
at all and if you only want n
on the last field, then just only use n
for the last field.– terdon♦
Jun 1 at 12:11
@WinEunuuchs2Unix please see updated answer. There's no need for multiple calls to
printf
at all and if you only want n
on the last field, then just only use n
for the last field.– terdon♦
Jun 1 at 12:11
@SergiyKolodyazhnyy excellent point of concatenating all fields into single
printf
call. By the same token a single echo
could be used with shorter code (no %s
) but I wonder if that is less efficient? Terdon thanks for your answer. Accepting now.– WinEunuuchs2Unix
Jun 1 at 15:11
@SergiyKolodyazhnyy excellent point of concatenating all fields into single
printf
call. By the same token a single echo
could be used with shorter code (no %s
) but I wonder if that is less efficient? Terdon thanks for your answer. Accepting now.– WinEunuuchs2Unix
Jun 1 at 15:11
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1147775%2fwhat-does-shellcheck-warning-sc2129-consider-using-cmd1-cmd2-file-inst%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
1
I think it's just suggesting doing a single write instead of multiple open/seek/write operations - but it might be clearer if you posted a larger sample of the script
– steeldriver
Jun 1 at 1:46
2
When the message is too terse, you can look at the wiki page for SC2129 which has a full length description with examples
– that other guy
Jun 1 at 8:09
@thatotherguy Your comment could be an answer if it included relevant parts of the link in the body. It would help others and I for one would gladly upvote it.
– WinEunuuchs2Unix
Jun 1 at 15:07