0

I have this bash script to find files recovered by PhotoRec (files I know are there, but because of their number I can't locate them one by one), but the find command doesn't return any results, if I run one by one the script lines, (except the while loop) every single one works... except find. I'm using SystemRescueCd, so I changed zsh to bash, but even so the strange behavior doesn't change.

Here is the bash script doesn't work as it should:

#!/bin/bash

findpath="/media/samsung/recup_dir.2/"
echo "FIND PATH: $findpath"

fileslistfile="/media/windows2/photorec-created-files.txt"
rm -rf "$fileslistfile"
touch "$fileslistfile"
echo "FILES LIST FILE: $fileslistfile"
echo "* * * * *"

while IFS= read -r line; do
    echo "LINE: $line"
    #echo "$line" >> "$fileslistfile"

    uncreatedfile=$(basename "$line")
    echo "UNCREATED FILE: $uncreatedfile"

    # this command doesn't return any result
    createdfile=$(find "$findpath" -name "$uncreatedfile" -print)
    echo "CREATED FILE: $createdfile"

    if [ "$createdfile" != "" ]; then
        echo "$createdfile" >> "$fileslistfile"
    else
        echo "$line" >> "$fileslistfile"
    fi
done < "/root/photorec-uncreated-files.txt"

exit 1

Here is the sequence of commands executed in CLI, that works OK:

findpath="/media/samsung/recup_dir.1/";
echo "FIND PATH: $findpath";

line="/media/samsung/recup_dir.1/inode_733583/R Jota - Pantomima-kCcgjM55IqA.mp4";
echo "LINE: $line";

uncreatedfile=$(basename "$line");
echo "UNCREATED FILE: $uncreatedfile";

# this command returns a result
createdfile=$(find "$findpath" -name "$uncreatedfile" -print);
echo "CREATED FILE: $createdfile";

if [ "$createdfile" = "" ]; 
then echo "FILE UNCREATED $line"; 
else echo "FILE CREATED $createdfile"; 
fi;

I played around with IFS variable in the while loop to IFS=$'\n' and with the -print and -print0 params in find (even dropping both), the result is always the same...

So. why the same command substitution behave differently? what is missing in the bash script?

  • Can you run script with debug mode? PS4='debug>' bash -x script.sh and see what is getting updated in the createdfile variable? – Inian Dec 3 '18 at 3:22
  • Or) Maybe you have a typo? In the first part, the path is set to findpath="/media/samsung/recup_dir.2/" and in the next as dir.1 as findpath="/media/samsung/recup_dir.1/"? – Inian Dec 3 '18 at 3:27
  • the script is running in debug mode but doesn't display any useful information. Yes there is a typo: it should be recup_dir.1 instead recup_dir.2, probably happens a minute before to post it, the behavior is the same. – abiyi Dec 3 '18 at 3:31
  • 1
    I gave a second look to the debugged commands and found out the cause: the source file photorec-uncreated-files.txt has all the lines like this: "Can't create file /media/samsung/recup_dir.1/inode_733583/R Jota - Pantomima-kCcgjM55IqA.mp4:" (with the sentence "Can't create ilfe " and a colon in the end of line, that's bad because $line is the variable used by $uncreatedfile which is used in the param -name of the find command, and that's why never work inside the bash script. Thanks for the advice @Inian !!! – abiyi Dec 3 '18 at 3:53
0

The source file photorec-uncreated-files.txt has all the lines like this:

Can't create file /media/samsung/recup_dir.1/inode_733583/R Jota - Pantomima-kCcgjM55IqA.mp4:

with the sentence "Can't create ilfe " at the beggining and a colon at the end. (Usually I delete those both using ViM, but seems that I've been forgetting to do that)

That's bad because $line is the variable used by $uncreatedfile to get the basename of the full path, which returns nothing, which is used then as a value of the param -name of the find command, which obviously returns nothing too, and that's why find never returns any result inside a bash script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.