Bash script: I am checking files by ls -1, I also want to loop if there are no files
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
New contributor
add a comment |
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
New contributor
Thank you very much
– user329491
Jan 2 at 22:51
add a comment |
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
New contributor
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
bash
New contributor
New contributor
edited 42 mins ago
Rui F Ribeiro
39.2k1479130
39.2k1479130
New contributor
asked Jan 2 at 20:48
user329491
6
6
New contributor
New contributor
Thank you very much
– user329491
Jan 2 at 22:51
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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 ...
}
Thank you very much
– user329491
Jan 2 at 22:51
add a comment |
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)?
Thank you very much.... I was able to use the set --.... worked fine
– user329491
Jan 2 at 22:52
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
});
}
});
user329491 is a new contributor. Be nice, and check out our Code of Conduct.
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%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
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 ...
}
Thank you very much
– user329491
Jan 2 at 22:51
add a comment |
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 ...
}
Thank you very much
– user329491
Jan 2 at 22:51
add a comment |
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 ...
}
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 ...
}
answered Jan 2 at 21:18
glenn jackman
50.4k570107
50.4k570107
Thank you very much
– user329491
Jan 2 at 22:51
add a comment |
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
add a comment |
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)?
Thank you very much.... I was able to use the set --.... worked fine
– user329491
Jan 2 at 22:52
add a comment |
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)?
Thank you very much.... I was able to use the set --.... worked fine
– user329491
Jan 2 at 22:52
add a comment |
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)?
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)?
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
add a comment |
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
add a comment |
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.
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.
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%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
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
Thank you very much
– user329491
Jan 2 at 22:51