How to repeat loop n times in BashBash File existence test is always trueCount number of times each line repeats itself, In case Insensitive mannerHow can I break out of a while loop, from within a nested case statement?while read loopHow to allow a bash shell script to loop for 3 retries before exiting (prompt for username/password)using else if in a bash alias(homework)I am writing an If then else to check for specific numeric value in a fileRepeat 3000 times a sequence of characters “OW HW1 HW2” in a columnHow do I loop an if statement in bash?write a shell script to change the file permission from read and write to read,write and execute permissions

Can one live in the U.S. and not use a credit card?

Create chunks from an array

Cycles on the torus

Why do phishing e-mails use faked e-mail addresses instead of the real one?

Automaton recognizing ambiguously accepted words of another automaton

What would be the most expensive material to an intergalactic society?

Are all players supposed to be able to see each others' character sheets?

What is this tube in a jet engine's air intake?

What does the Digital Threat scope actually do?

How should I solve this integral with changing parameters?

Traveling to heavily polluted city, what practical measures can I take to minimize impact?

"If + would" conditional in present perfect tense

Help! My Character is too much for her story!

Is "cogitate" used appropriately in "I cogitate that success relies on hard work"?

How do we create new idioms and use them in a novel?

Under what conditions can the right to remain silent be revoked in the USA?

What does *dead* mean in *What do you mean, dead?*?

Are these two graphs isomorphic? Why/Why not?

What should I do when a paper is published similar to my PhD thesis without citation?

Why restrict private health insurance?

Will expression retain the same definition if particle is changed?

When an outsider describes family relationships, which point of view are they using?

Short scifi story where reproductive organs are converted to produce "materials", pregnant protagonist is "found fit" to be a mother

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?



How to repeat loop n times in Bash


Bash File existence test is always trueCount number of times each line repeats itself, In case Insensitive mannerHow can I break out of a while loop, from within a nested case statement?while read loopHow to allow a bash shell script to loop for 3 retries before exiting (prompt for username/password)using else if in a bash alias(homework)I am writing an If then else to check for specific numeric value in a fileRepeat 3000 times a sequence of characters “OW HW1 HW2” in a columnHow do I loop an if statement in bash?write a shell script to change the file permission from read and write to read,write and execute permissions













6















I have below scenario like:



if [file exists]; then
exit
elif
recheck if file exist (max 10 times)
if found exit else recheck again as per counter
fi









share|improve this question
























  • This is a very basic feature in shells. Did you even research it?

    – Peschke
    Aug 5 '18 at 7:03











  • Yes. But not getting expected output from my code. Also want to write as abbreviated as possible

    – Rocky86
    Aug 5 '18 at 7:05






  • 1





    @Peschke, well, they'd need at least what, three basic features (loops, conditionals, testing the file, breaking out of a loop). At least the question is quite clear as it is. Though it could contain a sketch of what Rocky tried, but then someone would rewrite it full in the answers anyway. ;)

    – ilkkachu
    Aug 5 '18 at 7:07















6















I have below scenario like:



if [file exists]; then
exit
elif
recheck if file exist (max 10 times)
if found exit else recheck again as per counter
fi









share|improve this question
























  • This is a very basic feature in shells. Did you even research it?

    – Peschke
    Aug 5 '18 at 7:03











  • Yes. But not getting expected output from my code. Also want to write as abbreviated as possible

    – Rocky86
    Aug 5 '18 at 7:05






  • 1





    @Peschke, well, they'd need at least what, three basic features (loops, conditionals, testing the file, breaking out of a loop). At least the question is quite clear as it is. Though it could contain a sketch of what Rocky tried, but then someone would rewrite it full in the answers anyway. ;)

    – ilkkachu
    Aug 5 '18 at 7:07













6












6








6








I have below scenario like:



if [file exists]; then
exit
elif
recheck if file exist (max 10 times)
if found exit else recheck again as per counter
fi









share|improve this question
















I have below scenario like:



if [file exists]; then
exit
elif
recheck if file exist (max 10 times)
if found exit else recheck again as per counter
fi






bash shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 20 mins ago









Rui F Ribeiro

41.3k1481140




41.3k1481140










asked Aug 5 '18 at 6:31









Rocky86Rocky86

7119




7119












  • This is a very basic feature in shells. Did you even research it?

    – Peschke
    Aug 5 '18 at 7:03











  • Yes. But not getting expected output from my code. Also want to write as abbreviated as possible

    – Rocky86
    Aug 5 '18 at 7:05






  • 1





    @Peschke, well, they'd need at least what, three basic features (loops, conditionals, testing the file, breaking out of a loop). At least the question is quite clear as it is. Though it could contain a sketch of what Rocky tried, but then someone would rewrite it full in the answers anyway. ;)

    – ilkkachu
    Aug 5 '18 at 7:07

















  • This is a very basic feature in shells. Did you even research it?

    – Peschke
    Aug 5 '18 at 7:03











  • Yes. But not getting expected output from my code. Also want to write as abbreviated as possible

    – Rocky86
    Aug 5 '18 at 7:05






  • 1





    @Peschke, well, they'd need at least what, three basic features (loops, conditionals, testing the file, breaking out of a loop). At least the question is quite clear as it is. Though it could contain a sketch of what Rocky tried, but then someone would rewrite it full in the answers anyway. ;)

    – ilkkachu
    Aug 5 '18 at 7:07
















This is a very basic feature in shells. Did you even research it?

– Peschke
Aug 5 '18 at 7:03





This is a very basic feature in shells. Did you even research it?

– Peschke
Aug 5 '18 at 7:03













Yes. But not getting expected output from my code. Also want to write as abbreviated as possible

– Rocky86
Aug 5 '18 at 7:05





Yes. But not getting expected output from my code. Also want to write as abbreviated as possible

– Rocky86
Aug 5 '18 at 7:05




1




1





@Peschke, well, they'd need at least what, three basic features (loops, conditionals, testing the file, breaking out of a loop). At least the question is quite clear as it is. Though it could contain a sketch of what Rocky tried, but then someone would rewrite it full in the answers anyway. ;)

– ilkkachu
Aug 5 '18 at 7:07





@Peschke, well, they'd need at least what, three basic features (loops, conditionals, testing the file, breaking out of a loop). At least the question is quite clear as it is. Though it could contain a sketch of what Rocky tried, but then someone would rewrite it full in the answers anyway. ;)

– ilkkachu
Aug 5 '18 at 7:07










3 Answers
3






active

oldest

votes


















7














There are many ways to do this loop.



With ksh93 syntax (also supported by zsh and bash):



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
sleep 10
done


For any POSIX-like shell:



n=0
while [ "$n" -lt 10 ] && [ ! -e filename ]; do
n=$(( n + 1 ))
sleep 10
done


Both of the loops sleep 10 seconds in each iteration before testing the existence of the file again.



After the loop has finished, you will have to test for existence of the file a last time to figure out whether the loop exited due to running 10 times or due to the file appearing.



If you wish, and if you have access to inotify-tools, you may replace the sleep 10 call with



inotifywait -q -t 10 -e create ./ >/dev/null


This would wait for a file creation event to occur in the current directory, but would time out after 10 seconds. This way your loop would exit as soon as the given filename appeared (if it appeared).



The full code, with inotifywait (replace with sleep 10 if you don't want that), may look like



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
inotifywait -q -t 10 -e create ./ >/dev/null
done

if [ -e filename ]; then
echo 'file appeared!'
else
echo 'file did not turn up in time'
fi





share|improve this answer

























  • With inotify, you could almost replace the whole loop. Just test if the file is there, and if not, inotifywait for a 100 seconds. Almost, since the file could be created just between the test and the inotify, and you'd sleep for the full 100 seconds before timing out...

    – ilkkachu
    Aug 5 '18 at 7:12






  • 1





    @ilkkachu Yes, that's a good idea, but here I'm just using inotifywait as a drop-in replacement for sleep.

    – Kusalananda
    Aug 5 '18 at 7:15


















3














If the count is not a variable you can use brace expansion:



for i in 1..10 # you can also use 0..9
do
whatever
done


If the count is a variable you can use the seq command:



count=10
for i in $(seq $count)
do
whatever
done





share|improve this answer























  • I want to loop only if file is not found (max. 10 times). If found lets say 3rd time then exit successfully

    – Rocky86
    Aug 5 '18 at 6:49











  • @Rocky86 : This does not contradict the solution proposed by xenoid. Nobody forces you to count until the end ....

    – user1934428
    Aug 6 '18 at 9:46


















0














n=0
until [ "$((n+=1))" -gt 10 ]
do <exists? command exit
done
echo oh noes!


though test -e file && exit is more flexible






share|improve this answer























  • Why the question mark? Note that the behaviour for globs in the target of redirections varies between shells.

    – Stéphane Chazelas
    Aug 5 '18 at 6:57






  • 2





    Note that it has the side effect of opening the file, which for fifos for instance can be quite bad (worse with a symlink to /dev/watchdog on Linux for instance)

    – Stéphane Chazelas
    Aug 5 '18 at 6:58











  • Even in Bash, where this would look for a file like exists1 or such, it still prints a bunch of errors if/when a matching file isn't found. (Also it errors if there are multiple matches.) Any other shell I tested seems to give errors in any case...

    – ilkkachu
    Aug 5 '18 at 7:01











  • @ikkachu - yeah. that was kinda the point. if the error happens, the script reports. if stderr should be suppressed, suppress it done 2<>/dev/null. does bash does that scripted? i thought it only effed that up in an -interactive context. still, exists? is as much as a filler name as file. but yeah, i hate quoting in redirects - if screws so much up.

    – mikeserv
    Aug 5 '18 at 7:19












  • @Stéphane - no reason, really. but yeah, fifos, unreadables... thats why i noted test -e.

    – mikeserv
    Aug 5 '18 at 7:22










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



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460595%2fhow-to-repeat-loop-n-times-in-bash%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














There are many ways to do this loop.



With ksh93 syntax (also supported by zsh and bash):



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
sleep 10
done


For any POSIX-like shell:



n=0
while [ "$n" -lt 10 ] && [ ! -e filename ]; do
n=$(( n + 1 ))
sleep 10
done


Both of the loops sleep 10 seconds in each iteration before testing the existence of the file again.



After the loop has finished, you will have to test for existence of the file a last time to figure out whether the loop exited due to running 10 times or due to the file appearing.



If you wish, and if you have access to inotify-tools, you may replace the sleep 10 call with



inotifywait -q -t 10 -e create ./ >/dev/null


This would wait for a file creation event to occur in the current directory, but would time out after 10 seconds. This way your loop would exit as soon as the given filename appeared (if it appeared).



The full code, with inotifywait (replace with sleep 10 if you don't want that), may look like



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
inotifywait -q -t 10 -e create ./ >/dev/null
done

if [ -e filename ]; then
echo 'file appeared!'
else
echo 'file did not turn up in time'
fi





share|improve this answer

























  • With inotify, you could almost replace the whole loop. Just test if the file is there, and if not, inotifywait for a 100 seconds. Almost, since the file could be created just between the test and the inotify, and you'd sleep for the full 100 seconds before timing out...

    – ilkkachu
    Aug 5 '18 at 7:12






  • 1





    @ilkkachu Yes, that's a good idea, but here I'm just using inotifywait as a drop-in replacement for sleep.

    – Kusalananda
    Aug 5 '18 at 7:15















7














There are many ways to do this loop.



With ksh93 syntax (also supported by zsh and bash):



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
sleep 10
done


For any POSIX-like shell:



n=0
while [ "$n" -lt 10 ] && [ ! -e filename ]; do
n=$(( n + 1 ))
sleep 10
done


Both of the loops sleep 10 seconds in each iteration before testing the existence of the file again.



After the loop has finished, you will have to test for existence of the file a last time to figure out whether the loop exited due to running 10 times or due to the file appearing.



If you wish, and if you have access to inotify-tools, you may replace the sleep 10 call with



inotifywait -q -t 10 -e create ./ >/dev/null


This would wait for a file creation event to occur in the current directory, but would time out after 10 seconds. This way your loop would exit as soon as the given filename appeared (if it appeared).



The full code, with inotifywait (replace with sleep 10 if you don't want that), may look like



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
inotifywait -q -t 10 -e create ./ >/dev/null
done

if [ -e filename ]; then
echo 'file appeared!'
else
echo 'file did not turn up in time'
fi





share|improve this answer

























  • With inotify, you could almost replace the whole loop. Just test if the file is there, and if not, inotifywait for a 100 seconds. Almost, since the file could be created just between the test and the inotify, and you'd sleep for the full 100 seconds before timing out...

    – ilkkachu
    Aug 5 '18 at 7:12






  • 1





    @ilkkachu Yes, that's a good idea, but here I'm just using inotifywait as a drop-in replacement for sleep.

    – Kusalananda
    Aug 5 '18 at 7:15













7












7








7







There are many ways to do this loop.



With ksh93 syntax (also supported by zsh and bash):



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
sleep 10
done


For any POSIX-like shell:



n=0
while [ "$n" -lt 10 ] && [ ! -e filename ]; do
n=$(( n + 1 ))
sleep 10
done


Both of the loops sleep 10 seconds in each iteration before testing the existence of the file again.



After the loop has finished, you will have to test for existence of the file a last time to figure out whether the loop exited due to running 10 times or due to the file appearing.



If you wish, and if you have access to inotify-tools, you may replace the sleep 10 call with



inotifywait -q -t 10 -e create ./ >/dev/null


This would wait for a file creation event to occur in the current directory, but would time out after 10 seconds. This way your loop would exit as soon as the given filename appeared (if it appeared).



The full code, with inotifywait (replace with sleep 10 if you don't want that), may look like



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
inotifywait -q -t 10 -e create ./ >/dev/null
done

if [ -e filename ]; then
echo 'file appeared!'
else
echo 'file did not turn up in time'
fi





share|improve this answer















There are many ways to do this loop.



With ksh93 syntax (also supported by zsh and bash):



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
sleep 10
done


For any POSIX-like shell:



n=0
while [ "$n" -lt 10 ] && [ ! -e filename ]; do
n=$(( n + 1 ))
sleep 10
done


Both of the loops sleep 10 seconds in each iteration before testing the existence of the file again.



After the loop has finished, you will have to test for existence of the file a last time to figure out whether the loop exited due to running 10 times or due to the file appearing.



If you wish, and if you have access to inotify-tools, you may replace the sleep 10 call with



inotifywait -q -t 10 -e create ./ >/dev/null


This would wait for a file creation event to occur in the current directory, but would time out after 10 seconds. This way your loop would exit as soon as the given filename appeared (if it appeared).



The full code, with inotifywait (replace with sleep 10 if you don't want that), may look like



for (( i=0; i<10; ++i)); do
[ -e filename ] && break
inotifywait -q -t 10 -e create ./ >/dev/null
done

if [ -e filename ]; then
echo 'file appeared!'
else
echo 'file did not turn up in time'
fi






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 5 '18 at 7:54

























answered Aug 5 '18 at 6:37









KusalanandaKusalananda

134k17255418




134k17255418












  • With inotify, you could almost replace the whole loop. Just test if the file is there, and if not, inotifywait for a 100 seconds. Almost, since the file could be created just between the test and the inotify, and you'd sleep for the full 100 seconds before timing out...

    – ilkkachu
    Aug 5 '18 at 7:12






  • 1





    @ilkkachu Yes, that's a good idea, but here I'm just using inotifywait as a drop-in replacement for sleep.

    – Kusalananda
    Aug 5 '18 at 7:15

















  • With inotify, you could almost replace the whole loop. Just test if the file is there, and if not, inotifywait for a 100 seconds. Almost, since the file could be created just between the test and the inotify, and you'd sleep for the full 100 seconds before timing out...

    – ilkkachu
    Aug 5 '18 at 7:12






  • 1





    @ilkkachu Yes, that's a good idea, but here I'm just using inotifywait as a drop-in replacement for sleep.

    – Kusalananda
    Aug 5 '18 at 7:15
















With inotify, you could almost replace the whole loop. Just test if the file is there, and if not, inotifywait for a 100 seconds. Almost, since the file could be created just between the test and the inotify, and you'd sleep for the full 100 seconds before timing out...

– ilkkachu
Aug 5 '18 at 7:12





With inotify, you could almost replace the whole loop. Just test if the file is there, and if not, inotifywait for a 100 seconds. Almost, since the file could be created just between the test and the inotify, and you'd sleep for the full 100 seconds before timing out...

– ilkkachu
Aug 5 '18 at 7:12




1




1





@ilkkachu Yes, that's a good idea, but here I'm just using inotifywait as a drop-in replacement for sleep.

– Kusalananda
Aug 5 '18 at 7:15





@ilkkachu Yes, that's a good idea, but here I'm just using inotifywait as a drop-in replacement for sleep.

– Kusalananda
Aug 5 '18 at 7:15













3














If the count is not a variable you can use brace expansion:



for i in 1..10 # you can also use 0..9
do
whatever
done


If the count is a variable you can use the seq command:



count=10
for i in $(seq $count)
do
whatever
done





share|improve this answer























  • I want to loop only if file is not found (max. 10 times). If found lets say 3rd time then exit successfully

    – Rocky86
    Aug 5 '18 at 6:49











  • @Rocky86 : This does not contradict the solution proposed by xenoid. Nobody forces you to count until the end ....

    – user1934428
    Aug 6 '18 at 9:46















3














If the count is not a variable you can use brace expansion:



for i in 1..10 # you can also use 0..9
do
whatever
done


If the count is a variable you can use the seq command:



count=10
for i in $(seq $count)
do
whatever
done





share|improve this answer























  • I want to loop only if file is not found (max. 10 times). If found lets say 3rd time then exit successfully

    – Rocky86
    Aug 5 '18 at 6:49











  • @Rocky86 : This does not contradict the solution proposed by xenoid. Nobody forces you to count until the end ....

    – user1934428
    Aug 6 '18 at 9:46













3












3








3







If the count is not a variable you can use brace expansion:



for i in 1..10 # you can also use 0..9
do
whatever
done


If the count is a variable you can use the seq command:



count=10
for i in $(seq $count)
do
whatever
done





share|improve this answer













If the count is not a variable you can use brace expansion:



for i in 1..10 # you can also use 0..9
do
whatever
done


If the count is a variable you can use the seq command:



count=10
for i in $(seq $count)
do
whatever
done






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 5 '18 at 6:47









xenoidxenoid

3,1601725




3,1601725












  • I want to loop only if file is not found (max. 10 times). If found lets say 3rd time then exit successfully

    – Rocky86
    Aug 5 '18 at 6:49











  • @Rocky86 : This does not contradict the solution proposed by xenoid. Nobody forces you to count until the end ....

    – user1934428
    Aug 6 '18 at 9:46

















  • I want to loop only if file is not found (max. 10 times). If found lets say 3rd time then exit successfully

    – Rocky86
    Aug 5 '18 at 6:49











  • @Rocky86 : This does not contradict the solution proposed by xenoid. Nobody forces you to count until the end ....

    – user1934428
    Aug 6 '18 at 9:46
















I want to loop only if file is not found (max. 10 times). If found lets say 3rd time then exit successfully

– Rocky86
Aug 5 '18 at 6:49





I want to loop only if file is not found (max. 10 times). If found lets say 3rd time then exit successfully

– Rocky86
Aug 5 '18 at 6:49













@Rocky86 : This does not contradict the solution proposed by xenoid. Nobody forces you to count until the end ....

– user1934428
Aug 6 '18 at 9:46





@Rocky86 : This does not contradict the solution proposed by xenoid. Nobody forces you to count until the end ....

– user1934428
Aug 6 '18 at 9:46











0














n=0
until [ "$((n+=1))" -gt 10 ]
do <exists? command exit
done
echo oh noes!


though test -e file && exit is more flexible






share|improve this answer























  • Why the question mark? Note that the behaviour for globs in the target of redirections varies between shells.

    – Stéphane Chazelas
    Aug 5 '18 at 6:57






  • 2





    Note that it has the side effect of opening the file, which for fifos for instance can be quite bad (worse with a symlink to /dev/watchdog on Linux for instance)

    – Stéphane Chazelas
    Aug 5 '18 at 6:58











  • Even in Bash, where this would look for a file like exists1 or such, it still prints a bunch of errors if/when a matching file isn't found. (Also it errors if there are multiple matches.) Any other shell I tested seems to give errors in any case...

    – ilkkachu
    Aug 5 '18 at 7:01











  • @ikkachu - yeah. that was kinda the point. if the error happens, the script reports. if stderr should be suppressed, suppress it done 2<>/dev/null. does bash does that scripted? i thought it only effed that up in an -interactive context. still, exists? is as much as a filler name as file. but yeah, i hate quoting in redirects - if screws so much up.

    – mikeserv
    Aug 5 '18 at 7:19












  • @Stéphane - no reason, really. but yeah, fifos, unreadables... thats why i noted test -e.

    – mikeserv
    Aug 5 '18 at 7:22















0














n=0
until [ "$((n+=1))" -gt 10 ]
do <exists? command exit
done
echo oh noes!


though test -e file && exit is more flexible






share|improve this answer























  • Why the question mark? Note that the behaviour for globs in the target of redirections varies between shells.

    – Stéphane Chazelas
    Aug 5 '18 at 6:57






  • 2





    Note that it has the side effect of opening the file, which for fifos for instance can be quite bad (worse with a symlink to /dev/watchdog on Linux for instance)

    – Stéphane Chazelas
    Aug 5 '18 at 6:58











  • Even in Bash, where this would look for a file like exists1 or such, it still prints a bunch of errors if/when a matching file isn't found. (Also it errors if there are multiple matches.) Any other shell I tested seems to give errors in any case...

    – ilkkachu
    Aug 5 '18 at 7:01











  • @ikkachu - yeah. that was kinda the point. if the error happens, the script reports. if stderr should be suppressed, suppress it done 2<>/dev/null. does bash does that scripted? i thought it only effed that up in an -interactive context. still, exists? is as much as a filler name as file. but yeah, i hate quoting in redirects - if screws so much up.

    – mikeserv
    Aug 5 '18 at 7:19












  • @Stéphane - no reason, really. but yeah, fifos, unreadables... thats why i noted test -e.

    – mikeserv
    Aug 5 '18 at 7:22













0












0








0







n=0
until [ "$((n+=1))" -gt 10 ]
do <exists? command exit
done
echo oh noes!


though test -e file && exit is more flexible






share|improve this answer













n=0
until [ "$((n+=1))" -gt 10 ]
do <exists? command exit
done
echo oh noes!


though test -e file && exit is more flexible







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 5 '18 at 6:36









mikeservmikeserv

45.8k668160




45.8k668160












  • Why the question mark? Note that the behaviour for globs in the target of redirections varies between shells.

    – Stéphane Chazelas
    Aug 5 '18 at 6:57






  • 2





    Note that it has the side effect of opening the file, which for fifos for instance can be quite bad (worse with a symlink to /dev/watchdog on Linux for instance)

    – Stéphane Chazelas
    Aug 5 '18 at 6:58











  • Even in Bash, where this would look for a file like exists1 or such, it still prints a bunch of errors if/when a matching file isn't found. (Also it errors if there are multiple matches.) Any other shell I tested seems to give errors in any case...

    – ilkkachu
    Aug 5 '18 at 7:01











  • @ikkachu - yeah. that was kinda the point. if the error happens, the script reports. if stderr should be suppressed, suppress it done 2<>/dev/null. does bash does that scripted? i thought it only effed that up in an -interactive context. still, exists? is as much as a filler name as file. but yeah, i hate quoting in redirects - if screws so much up.

    – mikeserv
    Aug 5 '18 at 7:19












  • @Stéphane - no reason, really. but yeah, fifos, unreadables... thats why i noted test -e.

    – mikeserv
    Aug 5 '18 at 7:22

















  • Why the question mark? Note that the behaviour for globs in the target of redirections varies between shells.

    – Stéphane Chazelas
    Aug 5 '18 at 6:57






  • 2





    Note that it has the side effect of opening the file, which for fifos for instance can be quite bad (worse with a symlink to /dev/watchdog on Linux for instance)

    – Stéphane Chazelas
    Aug 5 '18 at 6:58











  • Even in Bash, where this would look for a file like exists1 or such, it still prints a bunch of errors if/when a matching file isn't found. (Also it errors if there are multiple matches.) Any other shell I tested seems to give errors in any case...

    – ilkkachu
    Aug 5 '18 at 7:01











  • @ikkachu - yeah. that was kinda the point. if the error happens, the script reports. if stderr should be suppressed, suppress it done 2<>/dev/null. does bash does that scripted? i thought it only effed that up in an -interactive context. still, exists? is as much as a filler name as file. but yeah, i hate quoting in redirects - if screws so much up.

    – mikeserv
    Aug 5 '18 at 7:19












  • @Stéphane - no reason, really. but yeah, fifos, unreadables... thats why i noted test -e.

    – mikeserv
    Aug 5 '18 at 7:22
















Why the question mark? Note that the behaviour for globs in the target of redirections varies between shells.

– Stéphane Chazelas
Aug 5 '18 at 6:57





Why the question mark? Note that the behaviour for globs in the target of redirections varies between shells.

– Stéphane Chazelas
Aug 5 '18 at 6:57




2




2





Note that it has the side effect of opening the file, which for fifos for instance can be quite bad (worse with a symlink to /dev/watchdog on Linux for instance)

– Stéphane Chazelas
Aug 5 '18 at 6:58





Note that it has the side effect of opening the file, which for fifos for instance can be quite bad (worse with a symlink to /dev/watchdog on Linux for instance)

– Stéphane Chazelas
Aug 5 '18 at 6:58













Even in Bash, where this would look for a file like exists1 or such, it still prints a bunch of errors if/when a matching file isn't found. (Also it errors if there are multiple matches.) Any other shell I tested seems to give errors in any case...

– ilkkachu
Aug 5 '18 at 7:01





Even in Bash, where this would look for a file like exists1 or such, it still prints a bunch of errors if/when a matching file isn't found. (Also it errors if there are multiple matches.) Any other shell I tested seems to give errors in any case...

– ilkkachu
Aug 5 '18 at 7:01













@ikkachu - yeah. that was kinda the point. if the error happens, the script reports. if stderr should be suppressed, suppress it done 2<>/dev/null. does bash does that scripted? i thought it only effed that up in an -interactive context. still, exists? is as much as a filler name as file. but yeah, i hate quoting in redirects - if screws so much up.

– mikeserv
Aug 5 '18 at 7:19






@ikkachu - yeah. that was kinda the point. if the error happens, the script reports. if stderr should be suppressed, suppress it done 2<>/dev/null. does bash does that scripted? i thought it only effed that up in an -interactive context. still, exists? is as much as a filler name as file. but yeah, i hate quoting in redirects - if screws so much up.

– mikeserv
Aug 5 '18 at 7:19














@Stéphane - no reason, really. but yeah, fifos, unreadables... thats why i noted test -e.

– mikeserv
Aug 5 '18 at 7:22





@Stéphane - no reason, really. but yeah, fifos, unreadables... thats why i noted test -e.

– mikeserv
Aug 5 '18 at 7:22

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460595%2fhow-to-repeat-loop-n-times-in-bash%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

Word for a person who has no opinion about whether god existsWord for having a definite opinion while simultaneously withholding judgment?What's the opposite of “newcomer? Is ”veteran" OK?What do you call an “atheist” who might believe in an afterlife?What's a word for someone who wants to voice opinions but not have them challenged?Word for someone who dismisses contrary opinions as irrational?Somone who thinks they are overly special/out of the ordinaryIs there a word, phrase or idiom for “a person who is incapable of thinking about the future”?The belief that a god is human-likeA word for a non-famous person/thing you have heard a lot aboutAdjective for a person who enjoys taking care of their appearance

2017 IndyCar Series Contents Series news Teams and drivers Schedule Season summary Footnotes References External links Navigation menu"INDYCAR: Initial 2018 bodywork concepts unveiled"the original"IndyCar confirms switch to Performance Friction brakes in 2017""AJ Foyt Racing will switch to Chevy"the original"Carlos Munoz, Conor Daly will drive for AJ Foyt Racing""Zach Veach's Indy 500 Debut Confirmed with Foyt""No mass exodus from Honda after Ganassi switch""Ex-F1 driver Sato joins Andretti Autosport for 2017 IndyCar season""IndyCar's Ryan Hunter-Reay, sponsor DHL paired through 2020""hhgregg and Andretti Autosport announce partnership for key races in 2016""INDYCAR: Rossi re-signs with Andretti"the original"McLaren Formula 1 - Fernando Alonso to race at Indy 500 with McLaren, Honda and Andretti Autosport""Shank will finally take part in Indy 500 with Harvey, Andretti | MotorSportsTalk""Andretti adds Jack Harvey to Indy 500 field""Ganassi switches to Honda power for 2017""INDYCAR: Chilton returns to Ganassi"the original"IndyCar silly season: Who's going where in 2017?""INDYCAR: Kanaan, NTT Data return to Ganassi"the original"Kimball to remain at Ganassi for 2017""Coyne confirms Bourdais for 2017 IndyCar season""Davison to sub for Bourdais in Indy 500"the original"Gutierrez confirmed for Detroit IndyCar debut""Gutierrez returns with Coyne for rest of 2017 season""Vautier to drive for Coyne at Texas"the original"INDYCAR: Coyne confirms Jones for 2017"the original"Pippa Mann returns to Coyne for Indy 500""Karam, Dreyer & Reinbold teaming up again for Indianapolis 500""Pigot to return to Ed Carpenter Racing""Hildebrand confirmed as full-time Ed Carpenter driver""Veach to replace injured Hildebrand at Barber"the originalNew Team Harding Racing Enters Chaves for 101st Indianapolis 500"Juncos Racing Announces Entry in 101st Running of the Indianapolis 500 :: Juncos Racing""Juncos confirms Pigot for Indy 500""Saavedra confirmed in Juncos' second 500 entry"the original"Lazier confirms Indy 500 run after son's USF2000 debut"the original"Claman DeMelo to race for RLLR at Sonoma"the original"Rahal signs Servia and ace engineer for 2017""IndyCar: Aleshin returns with Schmidt"the original"Aleshin replaced by Saavedra for Toronto""Jack Harvey will pilot SPM No. 7 car at Watkins Glen, Sonoma""Jay Howard confirmed in Tony Stewart's supported SPM Indy entry""INDYCAR: Newgarden to wave the flag at Penske"the original"Pagenaud opts for No. 1 in 2017"the original"Penske confirms Newgarden for 2017""Montoya to stay with Team Penske in 2017""Target leaving IndyCar after 27 seasons with Chip Ganassi""Cavin: IndyCar could see complete driver/team shakeup in 2017""End of the road for KV Racing?""KV Racing confirms closure, equipment sold to Juncos""Juncos confirms IndyCar Series entry"the original"Juncos readies IndyCar program, aims for '17 500"the original"Harding Racing to add Texas, Pocono to schedule"the original"Sato signs with Andretti Autosport for 2017""INDYCAR: Aleshin in Doubt at SPM"the original"Long Beach notebook: JR Hildebrand breaks hand""Hildebrand cleared to return at Phoenix"the original"Bourdais to undergo surgery on multiple fractures""Aleshin loses Schmidt Peterson IndyCar ride""Saavedra in at SPM for Pocono, Gateway"the original"Bourdais to make return at Gateway"the original"The IndyCar Grand Prix no longer is sponsored by Angie's List""2017 IndyCar Series rulebook""2017 Verizon IndyCar Series Official Rulebook"Official websiteeeeee

What was this official D&D 3.5e Lovecraft-flavored rulebook?What was this set of RPG tools called?As a first-time DM should I let my players play complex character classes and roles?Nymph's Kiss and the RelationshipWhat was the name of this Cleric Prestige Class that shapes metal with its bare hands?Are the 3.5e Dragonlance books third party or official works?What's up with the domain Vile Darkness?What was this 80s book about RPGs?What was the name of this Werewolf band?What book had Rituals to “upgrade” animal companions to keep them viable at higher levels?What was this RPG that had rules for player-owned businesses?