Bash script: I am checking files by ls -1, I also want to loop if there are no files












0














I need to check if there are 12 files landed in a dir. This loop works fine when there is a one file and it will loop 4 times with "sleep 300". but when there are no files at all, it fails and does not loop. what else can I add to make it loop even with NO files at all. In short, I want to check 20 mins for file delivery.



retry() {
attempt_num=0
while [[ `ls -1 *File*${JulianDate}.* | wc -l` -lt 12 ]]
do









share|improve this question









New contributor




user329491 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Thank you very much
    – user329491
    Jan 2 at 22:51
















0














I need to check if there are 12 files landed in a dir. This loop works fine when there is a one file and it will loop 4 times with "sleep 300". but when there are no files at all, it fails and does not loop. what else can I add to make it loop even with NO files at all. In short, I want to check 20 mins for file delivery.



retry() {
attempt_num=0
while [[ `ls -1 *File*${JulianDate}.* | wc -l` -lt 12 ]]
do









share|improve this question









New contributor




user329491 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Thank you very much
    – user329491
    Jan 2 at 22:51














0












0








0







I need to check if there are 12 files landed in a dir. This loop works fine when there is a one file and it will loop 4 times with "sleep 300". but when there are no files at all, it fails and does not loop. what else can I add to make it loop even with NO files at all. In short, I want to check 20 mins for file delivery.



retry() {
attempt_num=0
while [[ `ls -1 *File*${JulianDate}.* | wc -l` -lt 12 ]]
do









share|improve this question









New contributor




user329491 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I need to check if there are 12 files landed in a dir. This loop works fine when there is a one file and it will loop 4 times with "sleep 300". but when there are no files at all, it fails and does not loop. what else can I add to make it loop even with NO files at all. In short, I want to check 20 mins for file delivery.



retry() {
attempt_num=0
while [[ `ls -1 *File*${JulianDate}.* | wc -l` -lt 12 ]]
do






bash






share|improve this question









New contributor




user329491 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user329491 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 42 mins ago









Rui F Ribeiro

39.2k1479130




39.2k1479130






New contributor




user329491 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Jan 2 at 20:48









user329491

6




6




New contributor




user329491 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user329491 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user329491 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Thank you very much
    – user329491
    Jan 2 at 22:51


















  • Thank you very much
    – user329491
    Jan 2 at 22:51
















Thank you very much
– user329491
Jan 2 at 22:51




Thank you very much
– user329491
Jan 2 at 22:51










2 Answers
2






active

oldest

votes


















3














Don't parse ls output. Read the list of files into an array, and check the size of the array.



retry() {
while true; do
files=( *File*${JulianDate}.* )
(( ${#files[@]} >= 12 )) && break
sleep for some amount
done
do stuff with 12 or more files ...
}





share|improve this answer





















  • Thank you very much
    – user329491
    Jan 2 at 22:51



















3














waitforfiles () {
n=0
while [ "$n" -lt 4 ]; do
set -- *File*$JulianDate.*
[ "$#" -ge 12 ] && return 0
sleep 300
n=$(( n + 1 ))
done

return 1
}

if ! waitforfiles; then
echo 'Not enough files arrived in time.' >&2
exit 1
fi

# Do something here.


Don't parse the output of ls, it is only for you to look at. Instead, use the shell to match the names that you want to match, and then count the number of files that matches. The shell gives this to you for more or less free (in comparison to calling the external utilities ls and wc).



The function above will sleep for 300 seconds and try again, until the pattern matches 12 or more filenames or until the loop has run four times. It returns success (zero) or failure (non-zero) depending on whether the files arrived in time or not.



Related:




  • Why *not* parse `ls` (and what do to instead)?






share|improve this answer























  • Thank you very much.... I was able to use the set --.... worked fine
    – user329491
    Jan 2 at 22:52











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
});


}
});






user329491 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492089%2fbash-script-i-am-checking-files-by-ls-1-i-also-want-to-loop-if-there-are-no-f%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









3














Don't parse ls output. Read the list of files into an array, and check the size of the array.



retry() {
while true; do
files=( *File*${JulianDate}.* )
(( ${#files[@]} >= 12 )) && break
sleep for some amount
done
do stuff with 12 or more files ...
}





share|improve this answer





















  • Thank you very much
    – user329491
    Jan 2 at 22:51
















3














Don't parse ls output. Read the list of files into an array, and check the size of the array.



retry() {
while true; do
files=( *File*${JulianDate}.* )
(( ${#files[@]} >= 12 )) && break
sleep for some amount
done
do stuff with 12 or more files ...
}





share|improve this answer





















  • Thank you very much
    – user329491
    Jan 2 at 22:51














3












3








3






Don't parse ls output. Read the list of files into an array, and check the size of the array.



retry() {
while true; do
files=( *File*${JulianDate}.* )
(( ${#files[@]} >= 12 )) && break
sleep for some amount
done
do stuff with 12 or more files ...
}





share|improve this answer












Don't parse ls output. Read the list of files into an array, and check the size of the array.



retry() {
while true; do
files=( *File*${JulianDate}.* )
(( ${#files[@]} >= 12 )) && break
sleep for some amount
done
do stuff with 12 or more files ...
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 21:18









glenn jackman

50.4k570107




50.4k570107












  • Thank you very much
    – user329491
    Jan 2 at 22:51


















  • Thank you very much
    – user329491
    Jan 2 at 22:51
















Thank you very much
– user329491
Jan 2 at 22:51




Thank you very much
– user329491
Jan 2 at 22:51













3














waitforfiles () {
n=0
while [ "$n" -lt 4 ]; do
set -- *File*$JulianDate.*
[ "$#" -ge 12 ] && return 0
sleep 300
n=$(( n + 1 ))
done

return 1
}

if ! waitforfiles; then
echo 'Not enough files arrived in time.' >&2
exit 1
fi

# Do something here.


Don't parse the output of ls, it is only for you to look at. Instead, use the shell to match the names that you want to match, and then count the number of files that matches. The shell gives this to you for more or less free (in comparison to calling the external utilities ls and wc).



The function above will sleep for 300 seconds and try again, until the pattern matches 12 or more filenames or until the loop has run four times. It returns success (zero) or failure (non-zero) depending on whether the files arrived in time or not.



Related:




  • Why *not* parse `ls` (and what do to instead)?






share|improve this answer























  • Thank you very much.... I was able to use the set --.... worked fine
    – user329491
    Jan 2 at 22:52
















3














waitforfiles () {
n=0
while [ "$n" -lt 4 ]; do
set -- *File*$JulianDate.*
[ "$#" -ge 12 ] && return 0
sleep 300
n=$(( n + 1 ))
done

return 1
}

if ! waitforfiles; then
echo 'Not enough files arrived in time.' >&2
exit 1
fi

# Do something here.


Don't parse the output of ls, it is only for you to look at. Instead, use the shell to match the names that you want to match, and then count the number of files that matches. The shell gives this to you for more or less free (in comparison to calling the external utilities ls and wc).



The function above will sleep for 300 seconds and try again, until the pattern matches 12 or more filenames or until the loop has run four times. It returns success (zero) or failure (non-zero) depending on whether the files arrived in time or not.



Related:




  • Why *not* parse `ls` (and what do to instead)?






share|improve this answer























  • Thank you very much.... I was able to use the set --.... worked fine
    – user329491
    Jan 2 at 22:52














3












3








3






waitforfiles () {
n=0
while [ "$n" -lt 4 ]; do
set -- *File*$JulianDate.*
[ "$#" -ge 12 ] && return 0
sleep 300
n=$(( n + 1 ))
done

return 1
}

if ! waitforfiles; then
echo 'Not enough files arrived in time.' >&2
exit 1
fi

# Do something here.


Don't parse the output of ls, it is only for you to look at. Instead, use the shell to match the names that you want to match, and then count the number of files that matches. The shell gives this to you for more or less free (in comparison to calling the external utilities ls and wc).



The function above will sleep for 300 seconds and try again, until the pattern matches 12 or more filenames or until the loop has run four times. It returns success (zero) or failure (non-zero) depending on whether the files arrived in time or not.



Related:




  • Why *not* parse `ls` (and what do to instead)?






share|improve this answer














waitforfiles () {
n=0
while [ "$n" -lt 4 ]; do
set -- *File*$JulianDate.*
[ "$#" -ge 12 ] && return 0
sleep 300
n=$(( n + 1 ))
done

return 1
}

if ! waitforfiles; then
echo 'Not enough files arrived in time.' >&2
exit 1
fi

# Do something here.


Don't parse the output of ls, it is only for you to look at. Instead, use the shell to match the names that you want to match, and then count the number of files that matches. The shell gives this to you for more or less free (in comparison to calling the external utilities ls and wc).



The function above will sleep for 300 seconds and try again, until the pattern matches 12 or more filenames or until the loop has run four times. It returns success (zero) or failure (non-zero) depending on whether the files arrived in time or not.



Related:




  • Why *not* parse `ls` (and what do to instead)?







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 21:26

























answered Jan 2 at 21:20









Kusalananda

122k16230375




122k16230375












  • Thank you very much.... I was able to use the set --.... worked fine
    – user329491
    Jan 2 at 22:52


















  • Thank you very much.... I was able to use the set --.... worked fine
    – user329491
    Jan 2 at 22:52
















Thank you very much.... I was able to use the set --.... worked fine
– user329491
Jan 2 at 22:52




Thank you very much.... I was able to use the set --.... worked fine
– user329491
Jan 2 at 22:52










user329491 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















user329491 is a new contributor. Be nice, and check out our Code of Conduct.













user329491 is a new contributor. Be nice, and check out our Code of Conduct.












user329491 is a new contributor. Be nice, and check out our Code of Conduct.
















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.





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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492089%2fbash-script-i-am-checking-files-by-ls-1-i-also-want-to-loop-if-there-are-no-f%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

濃尾地震

How to rewrite equation of hyperbola in standard form

No ethernet ip address in my vocore2