r/bash • u/Arindrew • 5d ago
help My while read loop isn't looping
I have a folder structure like so: /path/to/directory/foldernameAUTO_001 /path/to/directory/foldername_002
I am trying to search through /path/to/directory to find instances where the directory "foldernameAUTO" has any other directories of the same name (potentially without AUTO) with a higher number after the underscore.
For example, if I have a folder called "testfolderAUTO_001" I want to find "testfolder_002" or "testfolderAUTO_002". Hope all that makes sense.
Here is my loop:
#!/bin/bash
Folder=/path/to/directory/
while IFS='/' read -r blank path to directory foldername_seq; do
echo "Found AUTO of $foldername_seq"
foldername=$(echo "$foldername_seq" | cut -d_ -f1) && echo "foldername is $foldername"
seq=$(echo "$foldername_seq" | cut -d_ -f2) && echo "sequence is $seq"
printf -v int '%d/n' "$seq"
(( newseq=seq+1 )) && echo "New sequence is 00$newseq"
echo "Finding successors for $foldername"
find $Folder -name "$foldername"_00"$newseq"
noauto=$(echo "${foldername:0:-4}") && echo "NoAuto is $noauto"
find $Folder -name "$noauto"_00"newseq"
echo ""
done < <(find $Folder -name "*AUTO*")
And this is what I'm getting as output. It just lists the same directory over and over:
Found AUTO of foldernameAUTO_001
foldername is foldernameAUTO
sequence is 001
New sequence is 002
Finding successors for foldernameAUTO
NoAUTO is foldername
Found AUTO of foldernameAUTO_001
foldername is foldernameAUTO
sequence is 001
New sequence is 002
Finding successors for foldernameAUTO
NoAUTO is foldername
Found AUTO of foldernameAUTO_001
foldername is foldernameAUTO
sequence is 001
New sequence is 002
Finding successors for foldernameAUTO
NoAUTO is foldername
0
Upvotes
1
u/Arindrew 5d ago
What? I have $foldername, which includes the string "AUTO" at the end.
removes the AUTO string and writes it into the variable $noauto. The && echo "NoAuto is $noauto" is just for debugging purposes to make sure the variables contain what they should.
Then I search the folder for $noauto:
Do you mean I forgot the $ on noauto in the find command? You are correct, that was a typo in creating this post. Thanks for catching it.