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
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
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
$(seq $count)
seq is only for old bash. {1..10} is for current (like last 10 years plus) bash
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
inotifywait as a drop-in replacement for sleep.
n=0
until [ "$((n+=1))" -gt 10 ]
do <exists? command exit
done
echo oh noes!
though test -e file && exit is more flexible
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...
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.
test -e.