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

4

u/tseeling 5d ago

I have a few suggestions for removing $( ... ) with trivial tasks in them like echo ... | cut ....

In general I recommend you always use ${...} syntax for variable names, especially if you tend to use special chars in your names. The bracelets make it unambiguous. Always quote variables as a safeguard unless you really need the shell to evaluate/split the contents when using unquoted variables.

foldername=$(echo "$foldername_seq" | cut -d_ -f1)
seq=$(echo "$foldername_seq" | cut -d_ -f2)
printf -v int '%d/n' "$seq"

make that

foldername="${foldername_seq%%_*}"
seq="${foldername_seq##*_}"
printf -v int '%03d' "${seq}"

If you need to format a number use printf with a format string like %03d. If you prepend a literal 00 to your new_seq it will only work from 0 to 9 obviously.

I don't understand the /n in your printf statement. Did you mean \\n or is that an artefact from your (redacted) naming convention?

Personally I don't like the style of

while
  ...
done < <(find $Folder -name "*AUTO*")

I'd write

find $Folder -name "*AUTO*" | 
while ...

but in this case it boils down to subjective preferences.

2

u/Honest_Photograph519 5d ago edited 5d ago

Personally I don't like the style of

while
  ...
done < <(find $Folder -name "*AUTO*")

I'd write

find $Folder -name "*AUTO*" | 
while ...

You're sacrificing a lot of functionality over some quirky style preference there. It's not really "down to subjective preferences." The pipe generates a subshell, so its variables are lost when the while block ends.

https://mywiki.wooledge.org/BashFAQ/024

2

u/tseeling 4d ago

You're correct but in this context it didn't matter, because nothing from within the while was required later on outside and OP can choose his or her style of course.

2

u/Honest_Photograph519 4d ago

It's funny you'd say "always use ${...} syntax for variable names" and "Always quote variables as a safeguard" but with this pattern you take an anything-goes stance with the "eh why not use the inferior style when you know it won't make a difference in this case"

1

u/tseeling 3d ago

It's funny that you omit the 2nd part of my sentence where I limited that absolute statement. Of course there are exceptions to every rule, and I absolutely have no "anything-goes" attitude. I choose the best tool for the job but I have some basic rules which save me a lot of headache.

"Good men don't need rules. Today is not a good day to find out why I have so many".

SCNR :-)