Shellscript can not find directory

6

I'm starting with shellscript, I have to do the checksum of some files, so I decided to automate things with a bash script. I made two scripts, one that uses a ls recursive in the directory set by me and ignores folders by taking only the * ('d') * files and passes it to an environment variable converting to a string, then a loop divides the converted output into rows, which are the paths of the files, passing them to another environment variable that is passed as parameter in the call of the second script, which receives this argument (file path), checksum and passes td to a file.txt, below the two scripts: first script:

#/bin/bash                                                      ###TASK3###

arquivos=$(ls -R $1 | egrep -v '^d')

for linha in $arquivos
        do
                bash ./task2.sh $linha
        done

second script:

#/bin/bash                                              #####TASK2####



checksum=$(hashdeep $1)
concatenado=''

for i in $checksum
        do
                concatenado+=$i

        done

IFS=',' read -ra ADDR <<< "$concatenado"

echo
echo '----Checksum FILE:' $1
echo '----Checksum HASH:' ${ADDR[4]}
echo
echo ${ADDR[4]} >> ~/Trampo/shell_scripts/txt2.txt

The output (summarized since there are many files):

  

/ home / douglas / Trap / shell_scripts / (copy) .png: No such file or   directory

     

---- Checksum FILE: (copy) .png   ---- HASH Checksum:

The error is as follows: in the case here I set the directory "~ / Images", but notice that in the output it returns the path where my .sh script is with the name of the file that is in the / Images , but I'm not sending it to / Images and not to / home / douglas / Trampo / shell_scripts / (copy) .png , by even because the copy.png file is not in this same directory ...

What am I doing wrong?

Thank you!

Then friend, I tried to change the files as you recommended me and other links that I saw net out ... they did not succeed, I tried two things ..

1st:

#!/bin/bash
find $1 -type f -exec ./task2.sh {} \;

2º:

#!/bin/bash
find $1 -type f -print0 | xargs -0 -n1 ./task2.sh

I also tried:

find $1 -type f -exec bash ./task2.sh {} \;

the latter worked partially, the output of it was something like:

  

---- Checksum FILE: / home / douglas / Images / Geotechnology: trends and challenges - Mozilla Firefox_003.png   ---- HASH Checksum:

     

---- Checksum FILE: /home/douglas/Images/Webcam/2016-10-27-001757.jpg   ---- Checksum HASH: 40c760ff07c60b6a37d279ecdeab26b8

     

---- Checksum FILE: /home/douglas/Images/2eVd0f1.jpg   ---- Checksum HASH: bd52f7a0a3f8845d15218f6de0436808

     

/ home / douglas / Images / Capture: No such file or directory   / home / douglas / Trampo / shell_scripts / from: No such file or directory   / home / douglas / Trap / shell_scripts / screen: No such file or directory   / home / douglas / Trampo / shell_scripts / from: No such file or directory   / home / douglas / Trampo / shell_scripts / 2016-10-17: No such file or   directory /home/douglas/Trampo/shell_scripts/23-26-56.png: No such   file or directory

    
asked by anonymous 01.11.2016 / 22:01

2 answers

2

The problem is that the first script ls returns the relative paths of the files. When these paths are consumed in the second script by hashdeep , the program assumes they are relative to the 'current' directory, which in this case would be the scripts directory.

I propose that instead use ls in the first script use find , passing as an absolute path parameter. Ex:

# find retorna caminhos absolutos quando o parâmetro é absoluto.
find ~/Imagens

In this way find will return absolute paths.

Note: Because absolute paths will be passed to egrep, it may be necessary to change the expression used.

    
02.11.2016 / 01:16
2

I was able to correct the error: I just changed the 1st attempt line to:

find $1 -type f -exec **bash** ./task2.sh "{}" \;
    
02.11.2016 / 16:38