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?
PS4='debug>' bash -x script.sh
and see what is getting updated in thecreatedfile
variable?findpath="/media/samsung/recup_dir.2/"
and in the next asdir.1
asfindpath="/media/samsung/recup_dir.1/"
?