I am trying to make a little bash script, that calls a command on each of my files in have in a file, found with the find command.
I want to be able to keep track of where the script stopped (it tends to crash) so i can take back from there. I managed to read my file, get the lines,... But currently i'm stuck at the for loop. I want to do a C style for loop, starting at the the last line i stopped at, increment by one, and do it as long as i'm smaller than the number of lines. I got this :
#!/bin/bash
LINES=$(wc -l < file.txt)
LASTLINE=$(grep -P '### Stop marker ###' file.txt | wc -l)
STARTFROM=$(($LINES - $LASTLINE))
for ((i = STARTFROM; i < LINES; i++));
do
echo "we are processing file number $i"
file=sed -n $i'p' file.txt
ocrmypdf [some stuff] -input $file
done
Here is an excerpt of what my file.txt looks like inside
./input_folder/hard_blurry.pdf
./input_folder/l_ordre_malte.pdf
### Stop marker ###
./input_folder/single_page.pdf
./input_folder/very_hard.pdf
When i run this i get... nothing. Bash doesn't enter the loop at all. I tried setting ints directly, and it worked, which tells me that the vars are bein read as string.
I tried all these ways to write my var :
for ((i = STARTFROM; i < LINES; i++));
for ((i = $((STARTFROM)); i < $((LINES)); i++));
for ((i = $(echo STARTFROM); i < $(echo LINES); i++));
and nothing works. I'm suprised that no erros are thrown as well. My Os is ubuntu 20.0.4
Its content is the path to the files i want to work with.
Any ideas ? Thanks
LASTLINEis always 1 or how many "Stop Makers" do you have?LASTLINE=$(grep -P '### Stop marker ###' file.txt | wc -l)is not the line number, but the number of Stop markers in your file. And-Pis not needed, you have a fixed string.set -o errexit(abort on nonzero exitstatus),set -o nounset(abort on unbound variable) andset -o pipefail(don't hide errors within pipes) could be useful. I actually set those with vim as automatic header whenever I create a new *.sh file. Two of those commands are explained here if you want more info: ricma.co/posts/tech/tutorials/bash-tip-tricks .set -xto print all commands the shell executes, including the values of variables in those commands. Switch debug output off withset +x.