Command substitution not working inside for loop condition
Clash Royale CLAN TAG#URR8PPP
for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done
Gives me an error:
-bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")
bash for loop-device
add a comment |
for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done
Gives me an error:
-bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")
bash for loop-device
add a comment |
for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done
Gives me an error:
-bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")
bash for loop-device
for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done
Gives me an error:
-bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")
bash for loop-device
bash for loop-device
asked Jan 22 at 17:03
user323587user323587
132
132
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The output of wc -l sedtest1
will be something like:
21 sedtest1
So the test will become something like a <= 1234 sedtest1
which is invalid.
Also, that does mean that the wc
command will be run for each iteration of the loop. If the content of the sedtest1
file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:
n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
for ((a = 0; a < n; a++)); do echo "$a"; done
I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.
Thanks for the sources, I'll look into them!
– user323587
Jan 22 at 17:30
add a comment |
Nevermind I was just being incredibly stupid...wc -l file
outputs as "x file".
All I had to do was this:for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done
1
Umm... Now you've made the code worse. Thewc
andawk
will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.
– Kusalananda
Jan 22 at 17:16
Oh I didn't say my answer was better, I just figured out the problem :D
– user323587
Jan 22 at 17:28
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f496011%2fcommand-substitution-not-working-inside-for-loop-condition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The output of wc -l sedtest1
will be something like:
21 sedtest1
So the test will become something like a <= 1234 sedtest1
which is invalid.
Also, that does mean that the wc
command will be run for each iteration of the loop. If the content of the sedtest1
file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:
n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
for ((a = 0; a < n; a++)); do echo "$a"; done
I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.
Thanks for the sources, I'll look into them!
– user323587
Jan 22 at 17:30
add a comment |
The output of wc -l sedtest1
will be something like:
21 sedtest1
So the test will become something like a <= 1234 sedtest1
which is invalid.
Also, that does mean that the wc
command will be run for each iteration of the loop. If the content of the sedtest1
file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:
n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
for ((a = 0; a < n; a++)); do echo "$a"; done
I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.
Thanks for the sources, I'll look into them!
– user323587
Jan 22 at 17:30
add a comment |
The output of wc -l sedtest1
will be something like:
21 sedtest1
So the test will become something like a <= 1234 sedtest1
which is invalid.
Also, that does mean that the wc
command will be run for each iteration of the loop. If the content of the sedtest1
file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:
n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
for ((a = 0; a < n; a++)); do echo "$a"; done
I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.
The output of wc -l sedtest1
will be something like:
21 sedtest1
So the test will become something like a <= 1234 sedtest1
which is invalid.
Also, that does mean that the wc
command will be run for each iteration of the loop. If the content of the sedtest1
file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:
n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
for ((a = 0; a < n; a++)); do echo "$a"; done
I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.
answered Jan 22 at 17:09
Stéphane ChazelasStéphane Chazelas
305k57574928
305k57574928
Thanks for the sources, I'll look into them!
– user323587
Jan 22 at 17:30
add a comment |
Thanks for the sources, I'll look into them!
– user323587
Jan 22 at 17:30
Thanks for the sources, I'll look into them!
– user323587
Jan 22 at 17:30
Thanks for the sources, I'll look into them!
– user323587
Jan 22 at 17:30
add a comment |
Nevermind I was just being incredibly stupid...wc -l file
outputs as "x file".
All I had to do was this:for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done
1
Umm... Now you've made the code worse. Thewc
andawk
will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.
– Kusalananda
Jan 22 at 17:16
Oh I didn't say my answer was better, I just figured out the problem :D
– user323587
Jan 22 at 17:28
add a comment |
Nevermind I was just being incredibly stupid...wc -l file
outputs as "x file".
All I had to do was this:for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done
1
Umm... Now you've made the code worse. Thewc
andawk
will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.
– Kusalananda
Jan 22 at 17:16
Oh I didn't say my answer was better, I just figured out the problem :D
– user323587
Jan 22 at 17:28
add a comment |
Nevermind I was just being incredibly stupid...wc -l file
outputs as "x file".
All I had to do was this:for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done
Nevermind I was just being incredibly stupid...wc -l file
outputs as "x file".
All I had to do was this:for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done
answered Jan 22 at 17:09
user323587user323587
132
132
1
Umm... Now you've made the code worse. Thewc
andawk
will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.
– Kusalananda
Jan 22 at 17:16
Oh I didn't say my answer was better, I just figured out the problem :D
– user323587
Jan 22 at 17:28
add a comment |
1
Umm... Now you've made the code worse. Thewc
andawk
will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.
– Kusalananda
Jan 22 at 17:16
Oh I didn't say my answer was better, I just figured out the problem :D
– user323587
Jan 22 at 17:28
1
1
Umm... Now you've made the code worse. The
wc
and awk
will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.– Kusalananda
Jan 22 at 17:16
Umm... Now you've made the code worse. The
wc
and awk
will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.– Kusalananda
Jan 22 at 17:16
Oh I didn't say my answer was better, I just figured out the problem :D
– user323587
Jan 22 at 17:28
Oh I didn't say my answer was better, I just figured out the problem :D
– user323587
Jan 22 at 17:28
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f496011%2fcommand-substitution-not-working-inside-for-loop-condition%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