0

I have a bash that reads as input a list of folder names (all numbers not letters) and tries to find the location of each. The Find command doesn't return anything to me at all. I do not know why. Whereas if I put the raw value without using a variable it works.

Code :

    #!/bin/bash
filename='bat.txt'
n=1
while read line;
do
lien=$(find ./ -type d -name "$line")
# for read each line
echo "line no. $n : $line"
n=$((n+1))
done < $filename

Bat.txt sample :

19223
12233
34434
34434
  • Please replace all images with its text. – Cyrus Oct 21 '21 at 12:07
  • Done ! Do you have a solution about this ? – Hamza YOUSSOUF Oct 21 '21 at 12:14
  • There is a typo: you assign to `lien`, and then access `$line`. – Benjamin W. Oct 21 '21 at 12:34
  • Your code isn't the same as on the picture. On the picture, you incorrectly escaped `$` and put the `*` between quotes, making `find` try to find directories literally named `*$line*`. – Benjamin W. Oct 21 '21 at 12:36
  • Even if I do that, it doesn't change the result. lien=$(find ./ -type d -name "$line") # for read each line echo "line no. $n : $lien" – Hamza YOUSSOUF Oct 21 '21 at 12:40
  • What does the directory structure look like? – Benjamin W. Oct 21 '21 at 13:26
  • Obviously, the directories are not there. On the first iteration, `line` equals (according to your example data) `19223`, and you are searching for a directory below your working directory, which has the name `19223`. Since this is not found, I conclude that such a directory does not exist. – user1934428 Oct 21 '21 at 13:52
  • `lien=$(find ./ -type d -name "$line")` should be `line=$(find ./ -type d -name "$line")` for this to work. Also, `#!/bin/bash` should not be indented. Other than that, it works as expected?! – Fonic Oct 21 '21 at 14:28
  • I tried replacing the $line with 19223 and it happens to find the folder. but when I put the $ line back it doesn't work anymore – Hamza YOUSSOUF Oct 21 '21 at 15:16
  • Add the command `set -x` at the beginning of the script (but after the shebang line) to get an execution trace as it runs, and see what that shows. – Gordon Davisson Oct 21 '21 at 17:27
  • @HamzaYOUSSOUF: please post the output of `cat -v bat.txt`. This might be a case of bad line endings. – Fonic Oct 21 '21 at 18:54
  • @Maxxim Output of cat -v bat.txt : 5453^M, 6405^M,... – Hamza YOUSSOUF Oct 22 '21 at 13:45
  • What is "^M" ? Each line finish with this – Hamza YOUSSOUF Oct 22 '21 at 13:46
  • @HamzaYOUSSOUF: As I suspected, the line endings of `bat.txt` are the issue. I added an answer to describe the solution. – Fonic Oct 22 '21 at 15:50

1 Answers1

1

Based on additional information provided via comments, the solution should be to correct the typos in the script:

#!/bin/bash
filename='bat.txt'
n=1
while read line;
do
line=$(find ./ -type d -name "$line")
# for read each line
echo "line no. $n : $line"
n=$((n+1))
done < $filename

and to convert the line endings of bat.txt from Windows/DOS to Linux/Unix:

sed -i 's/\r$//' bat.txt 

Additional information on this issue can be found here, various alternatives to perform the conversion can be found here.

Fonic
  • 2,625
  • 23
  • 20