Shellscript does not find the directory

Asked

Viewed 198 times

6

I’m starting with shellscript ,I have to checksum some files, so I decided to automate things with a bash script.. I made two scripts, one that uses one ls recursive in the directory set by me and ignores folders taking only the files*(’d')* and passes an environment variable converting to a string, then a loop splits the output converted into lines, which are the paths of the files,passing them in 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 as many files):

/home/Douglas/Trampo/shell_scripts/(copy). png: No such file or directory

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

The error is as follows: in the case here I have set the directory "~/Images", but note that in the output it returns the path where my script is. sh with the name of the file that is in the folder ~/Imagery,only I’m not sending him to ~/Imagery and not to /home/Douglas/Trampo/shell_scripts/(copy). png,for example, up to why the file png copy. it’s not in that directory anyway...

What am I doing wrong?

Grateful!


So friend,I tried to change the files as you recommended me and other links I saw the net out...I unsuccessfully tried two things..

first:

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

2nd:

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

tried as well:

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

the latter functioned partially,his output became something like:

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

----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/de: No such file or directory /home/Douglas/Trampo/shell_scripts/tela: No such file or directory /home/Douglas/Trampo/shell_scripts/de: 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

  • Hi! This account is the same person as the author of the reply/comment below?

2 answers

2

I was able to correct the error: I just changed the line from the first attempt to:

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

2

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

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

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

In this way find will return absolute paths.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.