Este é um post qna (não precisa de ajuda, só queria ajudar os outros).
Recentemente trabalhei com alguns arquivos e tive dificuldade em ler os arquivos
#version1 (error on files with spaces)
arrFiles[0]=0
folderLocation="/home/thenok/.local/share/recent"
for i in $(find "$folderLocation" ! -name "*~" -type f); do
arrFiles[0]=$[arrFiles[0]+1] arrFiles[arrFiles[0]]="$i"
echo arrFiles at pos ${arrFiles[0]} found ${arrFiles[arrFiles[0]]}
done
encontrou uma (boa) solução:
#version2 works (ok but the global variable change bothered me)
IFS=$'\n' # make newlines the only separator
arrFiles[0]=0
folderLocation="/home/thenok/.local/share/recent"
for i in $(find "$folderLocation" ! -name "*~" -type f); do
arrFiles[0]=$[arrFiles[0]+1] arrFiles[arrFiles[0]]="$i"
echo arrFiles at pos ${arrFiles[0]} found ${arrFiles[arrFiles[0]]}
done
Como eu não queria alterar a variável global do IFS, tentei usar o comando exec em find, mas a modificação de variáveis era um beco sem saída (nenhum código para a versão 3, perdi)
Depois de navegar um pouco, descobri que podemos usar a leitura no canal:
#version4 (horrible side effect of piping is that all variables altered between do and done do not last, maybe some people will like that)
arrFileNames[0]=0
folderLocation="/home/thenok/.local/share/recent"
find "$folderLocation" ! -name "*~" -type f | while read i; do
arrFileNames[++arrFileNames[0]]=${i} ;
echo arrFiles at pos ${arrFiles[0]} found ${arrFiles[arrFiles[0]]}
done
e conseguiu fazê-lo funcionar
#version5 works perfectly
folderLocation="/home/thenok/.local/share/recent"
arrFileNames[0]=0 && while read i; do arrFileNames[++arrFileNames[0]]=${i} ;
#insert custom code
echo arrFiles at pos ${arrFiles[0]} found ${arrFiles[arrFiles[0]]}
#end custom code
done < <( find "$folderLocation" ! -name "*~" -type f)
Depuração:
find "$folderLocation" ! -name "*~" -type f
#will show all files that match search location, '-type f' files not folders, '! -name "*~"' avoids backup files (usually created by office or text editors as a backup in case writing to disk failed)
#to search for a specific file name add '-name "*.pdf"' for all pdf files
#for folder path add '-path "*/torrent/*"'
#add ! to match opposite '! -path "*/torrent/*" ! -name "*.pdf"' folder name must not contain folder called torrent and must not be a pdf
#for only current folder add ' -maxdepth 1' as first argument ie 'find "$folderLocation" -maxdepth 1 ! -name "*~" -type f'
#for more complex conditions you can use '\(' '\)' '!' '-or' '-and'
find "$folderLocation" -maxdepth 1 \(\( ! -name "*~" -or -path "*/torrent/*" \) -and \( -name "*~" -and ! -path "*/torrent/*" \)\) -type f'