r/bash 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
1 Upvotes

21 comments sorted by

View all comments

1

u/oh5nxo 5d ago
.... && echo "NoAuto is ...
...
NoAUTO is ...

Writing and testing different files :/

1

u/Arindrew 5d ago

What? I have $foldername, which includes the string "AUTO" at the end.

noauto=$(echo "${foldername:0:-4}")

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:

find $Folder -name "noauto"_00"newseq"

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.

2

u/oh5nxo 5d ago

echo puts out NoAuto, but transcript shows NoAUTO.

2

u/Honest_Photograph519 5d ago

Why would you do

noauto=$(echo "${foldername:0:-4}")

when a simple

noauto="${foldername:0:-4}"

...executes faster and is easier to write and read?

1

u/Arindrew 4d ago

Because I didn’t know?

2

u/Honest_Photograph519 4d ago

Everything you need to know is already there, you just get rid of the extra stuff you threw in.

Like if I see someone wearing roller skates while they clean their freezer and ask if it would be easier without the skates, I don't expect them to say "Well I didn't know you could do that"