I have a bash script that does stuff with files that are outputted by a GUI. I have it sleep for some time and if it finds files then it goes back through the loop. However, I want the script to exit if it doesn't find any files after the sleep period but if files are generated during the sleep period then to go back through the loop. But the script gets stuck in the loop to keep sleeping if it doesn't detect any files. My script looks like this:
#!/bin/bash
current_directory=$(pwd)
folder=1
while [ $folder -le 100 ]; do
fast5files=$(ls $current_directory/fast5/ | grep ".fast5" | wc -l)
if [ $fast5files -ge 1 ]
then
mkdir $current_directory/fast5/$folder
mv $current_directory/fast5/*.fast5 $current_directory/fast5/$folder/
program does something to fast5files here
another program does something to fast5files here
sleep $1 #variable set by user
((folder ))
elif [ $fast5files -eq 0 ]
then
echo "Couldn't find enough fast5 files. Waiting for 15 seconds"
sleep 15
elif [ $fast5files -eq 0 ]
then
echo "Exiting script due to not enough fast5 files"
exit 1
fi
done
CodePudding user response:
You set folder
and check it, but never modify it.
folder=1
while [ $folder -le 100 ]; do ...
If it never gets modified, the loop will never end without a manual break
or exit
or something like that.
Maybe something like -
countFiles() { local lst=( fast5/*.fast5* );
if [[ -e $lst ]]; then cnt=${#lst[@]}; else return 1; fi;
return $((!cnt));
}
while true; do
if countFiles
then echo doing stuff :$cnt:
flag=0
elif (( flag > 0 ))
then echo "Exiting script due to not enough fast5 files"
exit 1
else (( flag))
echo "Couldn't find enough fast5 files. Waiting for 15 seconds"
sleep 15
fi
done
This is quick and sloppy, setting a global from inside a function, etc., but should give you some ideas.
CodePudding user response:
you need to add the exit after the sleep statment. you have created a new elif branch that will never match as it is already matched earlier
#/bin/bash
current_directory=$(pwd)
folder=1
while [ $folder -le 100 ]; do
fast5files=$(ls $current_directory/fast5/ | grep ".fast5" | wc -l)
if [ $fast5files -ge 1 ]
then
# do something here
return 1
elif [ $fast5files -eq 0 ]
then
echo "Couldn't find enough fast5 files. Waiting for 15 seconds"
sleep 15
echo "now exiting"
exit 1
fi
That said i don't know why you still want to sleep, and not just exit, you can also just update the loop statement to exit when fast5files is 0 i.e.
#/bin/bash
current_directory=$(pwd)
folder=1
fast5files=1
while [ $folder -le 100 -o $fast5files -ge 0 ]; do
# do stuff
sleep 15
fast5files=$(find -type f -printf '.' -name '*.fast5' | wc -l)
done
fyi nothing in the current script updates the folder parameter so that part of the statement will always be true
CodePudding user response:
You don't need to check if *.fast5 files exist before mv
ing them.
Just attempt to mv
them and check the exit status of mv
. Probably this is what you're looking for:
#!/bin/bash
slept=0
n=1
while ((n <= 100))
do
mkdir -p fast5/$n || exit
if mv fast5/*.fast5 fast5/$n 2>/dev/null
then
(( n))
slept=0
elif ((!slept))
then
sleep 15
slept=1
else
break
fi
done