Pergunta

This program:

#!/bin/bash

find teste1 -type f -iname "**" | while read -r firstResult
do
find teste2 -type f -iname "**" | while read -r secondResult
do
firstName=${firstResult##*[/|\\]}
secondName=${secondResult##*[/|\\]}
if [[ $firstName == $secondName ]]; then
echo "$firstResult" "$secondResult" >> equal.lst
else
echo "$firstResult" "$secondResult" >> notEqual.lst
fi
done
done

I am having a bit of a problem with it, it's working quite alright, however, when the folder is as this example: /teste1/TESTE.pub /teste2/TEstE.pub, it doesn't put the file on "equal". How can I do this? I've been trying to do the find without case sensitive, meaning it should work, but it just doesn't acknowledge.

Please help.

Maybe I should just "transform" all the files names to one of the cases and then do that search? You think that would solve my problem? In terms of logic, it would work since all files would have the same casing.

Foi útil?

Solução

No need to use tr, bash has its own built in case conversion (${var,,}). Also, no need for -iname ** with find, leaving this out will match all files by default.

#!/bin/bash

find teste1 -type f | while read -r firstResult
do
  find teste2 -type f | while read -r secondResult
  do
    firstName="${firstResult##*[/|\\]}"
    secondName="${secondResult##*[/|\\]}"

    if [[ "${firstName,,}" == "${secondName,,}" ]]; then
      echo "$firstResult" "$secondResult" >> equal.lst
    else
      echo "$firstResult" "$secondResult" >> notEqual.lst
    fi
  done
done

Outras dicas

Ok, so, I solved the problem I was having by changing all the file names to lower case.

#!/bin/bash

find teste1 -type f | tr [A-Z] [a-z] | while read -r firstResult
do
find teste2 -type f | tr [A-Z] [a-z] | while read -r secondResult
do
firstName=${firstResult##*[/|\\]}
secondName=${secondResult##*[/|\\]}
if [[ $firstName == $secondName ]]; then
echo "$firstResult" "$secondResult" >> equal.lst
else
echo "$firstResult" "$secondResult" >> notEqual.lst
fi
done
done

It is now matching and saving the files exactly as I wanted. In case other people wonder, if you want this FIND to be case-sensitive, just remove the tr [A-Z] [a-z] command.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top