-2

I would like each row generated by my command to be an element of an array but when I try to see how many elements are in my array, it tells me that there is only one.

#!/bin/bash

var=$(find "$1" 2>/dev/null)
echo "$var"
t=("$var")
echo "$t"
echo "${t[1]}"
echo "${#t[*]}"

In fact I would like to iterate on my elements. I tried another way but it doesn't work either :

#!/bin/bash

find "$1" 2>/dev/null | while read line
do
        code_fichier=$(md5sum "$line" 2>/dev/null)
        echo "$code_fichier" >> list_md5sum.txt 2> /dev/null
done

cat list_md5sum.txt | while read line
do
        var=$(grep "$(echo "$line" | cut -f1 -d" ")" list_md5sum.txt)
        echo "$var"
done

rm list_md5sum.txt

I want to have a grep on the line element only once but, by iterating, the grep is done several times (the number of lines there are in the file) but I don't see how to avoid that

I thank you for your help and wish you a good day

Cyrus
  • 77,979
  • 13
  • 71
  • 125
bkbn
  • 35
  • 4
  • `t=("$var")` very literally assigns a single quoted value to the array. – tripleee May 01 '22 at 18:43
  • Why do you need to save the values to a variable instead of simply iterate over them? – tripleee May 01 '22 at 18:44
  • The first index of an array is zero. – glenn jackman May 01 '22 at 18:46
  • It's really not clear what you hope this code should do. Could you please [edit] to provide a prose description of the end goal and how you were hoping this code was a way to get there? Code which doesn't do what you want is a poor way to explain what you actually want. – tripleee May 01 '22 at 18:46

1 Answers1

2

Reading between the lines, I'm guessing you are trying to find duplicate files by hashing. Perhaps try this.

find "$1" -exec md5sum {} + 2>/dev/null |
sort |
awk '$1==hash { if(p) print p; p="";
    print; next }
  { hash=$1; p=$0 }'

This will break in interesting ways if you have file names with newlines in them. For a fuller treatment, perhaps see https://mywiki.wooledge.org/BashFAQ/020

tripleee
  • 158,107
  • 27
  • 234
  • 292