While loop breaks after the first iteration
up vote
0
down vote
favorite
This is my script which breaks on while loop after the first iteration
while IFS='' read -r line
do
cd /root/clair-container-scan
image_name=$(echo $line|cut -d '/' -f2|cut -d ':' -f1)
sh clair-container-scan.sh $line >> /root/clair-container-scan/$image_name.log
done < "$1"
The $1 refers to the file which has the below inputs to it.
kardasz/mysql:latest
visualops/httpd:latest
Most of them refer this to the STDOUT problem, so have already tried changing the >> redirection to ">" , "&>" , "&>>" but still didn't work.
The command that redirects the output to .log file will run for more than a minute. Sometimes it will not even run the first iteration and will simply give an empty output.
Can someone please help me understand what is wrong in the script?
shell while-loop stdout
add a comment |
up vote
0
down vote
favorite
This is my script which breaks on while loop after the first iteration
while IFS='' read -r line
do
cd /root/clair-container-scan
image_name=$(echo $line|cut -d '/' -f2|cut -d ':' -f1)
sh clair-container-scan.sh $line >> /root/clair-container-scan/$image_name.log
done < "$1"
The $1 refers to the file which has the below inputs to it.
kardasz/mysql:latest
visualops/httpd:latest
Most of them refer this to the STDOUT problem, so have already tried changing the >> redirection to ">" , "&>" , "&>>" but still didn't work.
The command that redirects the output to .log file will run for more than a minute. Sometimes it will not even run the first iteration and will simply give an empty output.
Can someone please help me understand what is wrong in the script?
shell while-loop stdout
we would need to see an Minimal, Complete, and Verifiable example version of yourclair-container-scan.sh, it's quite possible something is wrong and causing anexitthere. Good luck.
– shellter
Oct 11 at 14:47
I would also recommend doing some debugging on your own. First run your code (all of it) thru shellcheck.net . Then if those fixes don't resolve your problem, addecho "line=$line"andecho "image_name=$image_name"in appropriate places to see what is happening. If that doesn't highlight where the problem is, then addset -xbefore thewhileloop andset +xhave the while loop. You may need to add the same inside of yourclair-container-scan.sh. Good luck.
– shellter
Oct 11 at 14:50
Will you while loop work as expected when you replacesh clair-container-scan.sh $linewithecho "line=${line}."
– Walter A
Oct 11 at 22:03
Consider redirecting the standard input ofclair-container-scan.shfrom/dev/null. If it reads standard input, then your main loop won't get a chance. You could also consider reading from a file descriptor other than standard input with theread -u Ncommand.
– Jonathan Leffler
Oct 12 at 3:12
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is my script which breaks on while loop after the first iteration
while IFS='' read -r line
do
cd /root/clair-container-scan
image_name=$(echo $line|cut -d '/' -f2|cut -d ':' -f1)
sh clair-container-scan.sh $line >> /root/clair-container-scan/$image_name.log
done < "$1"
The $1 refers to the file which has the below inputs to it.
kardasz/mysql:latest
visualops/httpd:latest
Most of them refer this to the STDOUT problem, so have already tried changing the >> redirection to ">" , "&>" , "&>>" but still didn't work.
The command that redirects the output to .log file will run for more than a minute. Sometimes it will not even run the first iteration and will simply give an empty output.
Can someone please help me understand what is wrong in the script?
shell while-loop stdout
This is my script which breaks on while loop after the first iteration
while IFS='' read -r line
do
cd /root/clair-container-scan
image_name=$(echo $line|cut -d '/' -f2|cut -d ':' -f1)
sh clair-container-scan.sh $line >> /root/clair-container-scan/$image_name.log
done < "$1"
The $1 refers to the file which has the below inputs to it.
kardasz/mysql:latest
visualops/httpd:latest
Most of them refer this to the STDOUT problem, so have already tried changing the >> redirection to ">" , "&>" , "&>>" but still didn't work.
The command that redirects the output to .log file will run for more than a minute. Sometimes it will not even run the first iteration and will simply give an empty output.
Can someone please help me understand what is wrong in the script?
shell while-loop stdout
shell while-loop stdout
edited Nov 12 at 5:35
u__
2,572929
2,572929
asked Oct 11 at 12:05
Nishanth
85
85
we would need to see an Minimal, Complete, and Verifiable example version of yourclair-container-scan.sh, it's quite possible something is wrong and causing anexitthere. Good luck.
– shellter
Oct 11 at 14:47
I would also recommend doing some debugging on your own. First run your code (all of it) thru shellcheck.net . Then if those fixes don't resolve your problem, addecho "line=$line"andecho "image_name=$image_name"in appropriate places to see what is happening. If that doesn't highlight where the problem is, then addset -xbefore thewhileloop andset +xhave the while loop. You may need to add the same inside of yourclair-container-scan.sh. Good luck.
– shellter
Oct 11 at 14:50
Will you while loop work as expected when you replacesh clair-container-scan.sh $linewithecho "line=${line}."
– Walter A
Oct 11 at 22:03
Consider redirecting the standard input ofclair-container-scan.shfrom/dev/null. If it reads standard input, then your main loop won't get a chance. You could also consider reading from a file descriptor other than standard input with theread -u Ncommand.
– Jonathan Leffler
Oct 12 at 3:12
add a comment |
we would need to see an Minimal, Complete, and Verifiable example version of yourclair-container-scan.sh, it's quite possible something is wrong and causing anexitthere. Good luck.
– shellter
Oct 11 at 14:47
I would also recommend doing some debugging on your own. First run your code (all of it) thru shellcheck.net . Then if those fixes don't resolve your problem, addecho "line=$line"andecho "image_name=$image_name"in appropriate places to see what is happening. If that doesn't highlight where the problem is, then addset -xbefore thewhileloop andset +xhave the while loop. You may need to add the same inside of yourclair-container-scan.sh. Good luck.
– shellter
Oct 11 at 14:50
Will you while loop work as expected when you replacesh clair-container-scan.sh $linewithecho "line=${line}."
– Walter A
Oct 11 at 22:03
Consider redirecting the standard input ofclair-container-scan.shfrom/dev/null. If it reads standard input, then your main loop won't get a chance. You could also consider reading from a file descriptor other than standard input with theread -u Ncommand.
– Jonathan Leffler
Oct 12 at 3:12
we would need to see an Minimal, Complete, and Verifiable example version of your
clair-container-scan.sh, it's quite possible something is wrong and causing an exit there. Good luck.– shellter
Oct 11 at 14:47
we would need to see an Minimal, Complete, and Verifiable example version of your
clair-container-scan.sh, it's quite possible something is wrong and causing an exit there. Good luck.– shellter
Oct 11 at 14:47
I would also recommend doing some debugging on your own. First run your code (all of it) thru shellcheck.net . Then if those fixes don't resolve your problem, add
echo "line=$line" and echo "image_name=$image_name" in appropriate places to see what is happening. If that doesn't highlight where the problem is, then add set -x before the while loop and set +x have the while loop. You may need to add the same inside of your clair-container-scan.sh. Good luck.– shellter
Oct 11 at 14:50
I would also recommend doing some debugging on your own. First run your code (all of it) thru shellcheck.net . Then if those fixes don't resolve your problem, add
echo "line=$line" and echo "image_name=$image_name" in appropriate places to see what is happening. If that doesn't highlight where the problem is, then add set -x before the while loop and set +x have the while loop. You may need to add the same inside of your clair-container-scan.sh. Good luck.– shellter
Oct 11 at 14:50
Will you while loop work as expected when you replace
sh clair-container-scan.sh $line with echo "line=${line}."– Walter A
Oct 11 at 22:03
Will you while loop work as expected when you replace
sh clair-container-scan.sh $line with echo "line=${line}."– Walter A
Oct 11 at 22:03
Consider redirecting the standard input of
clair-container-scan.sh from /dev/null. If it reads standard input, then your main loop won't get a chance. You could also consider reading from a file descriptor other than standard input with the read -u N command.– Jonathan Leffler
Oct 12 at 3:12
Consider redirecting the standard input of
clair-container-scan.sh from /dev/null. If it reads standard input, then your main loop won't get a chance. You could also consider reading from a file descriptor other than standard input with the read -u N command.– Jonathan Leffler
Oct 12 at 3:12
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Resolved the issue by using for loop and read array. The problem is actually with lines the while loop read, it has special characters like "/ :"
filename=$1
readarray images < "$filename"
for line in "${images[@]}"
do
echo ""
done
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
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%2fstackoverflow.com%2fquestions%2f52759678%2fwhile-loop-breaks-after-the-first-iteration%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
up vote
0
down vote
accepted
Resolved the issue by using for loop and read array. The problem is actually with lines the while loop read, it has special characters like "/ :"
filename=$1
readarray images < "$filename"
for line in "${images[@]}"
do
echo ""
done
add a comment |
up vote
0
down vote
accepted
Resolved the issue by using for loop and read array. The problem is actually with lines the while loop read, it has special characters like "/ :"
filename=$1
readarray images < "$filename"
for line in "${images[@]}"
do
echo ""
done
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Resolved the issue by using for loop and read array. The problem is actually with lines the while loop read, it has special characters like "/ :"
filename=$1
readarray images < "$filename"
for line in "${images[@]}"
do
echo ""
done
Resolved the issue by using for loop and read array. The problem is actually with lines the while loop read, it has special characters like "/ :"
filename=$1
readarray images < "$filename"
for line in "${images[@]}"
do
echo ""
done
answered Nov 12 at 5:30
Nishanth
85
85
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fstackoverflow.com%2fquestions%2f52759678%2fwhile-loop-breaks-after-the-first-iteration%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
we would need to see an Minimal, Complete, and Verifiable example version of your
clair-container-scan.sh, it's quite possible something is wrong and causing anexitthere. Good luck.– shellter
Oct 11 at 14:47
I would also recommend doing some debugging on your own. First run your code (all of it) thru shellcheck.net . Then if those fixes don't resolve your problem, add
echo "line=$line"andecho "image_name=$image_name"in appropriate places to see what is happening. If that doesn't highlight where the problem is, then addset -xbefore thewhileloop andset +xhave the while loop. You may need to add the same inside of yourclair-container-scan.sh. Good luck.– shellter
Oct 11 at 14:50
Will you while loop work as expected when you replace
sh clair-container-scan.sh $linewithecho "line=${line}."– Walter A
Oct 11 at 22:03
Consider redirecting the standard input of
clair-container-scan.shfrom/dev/null. If it reads standard input, then your main loop won't get a chance. You could also consider reading from a file descriptor other than standard input with theread -u Ncommand.– Jonathan Leffler
Oct 12 at 3:12