Find in command substitution: Empty result inside while, result in command line











up vote
0
down vote

favorite












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?










share|improve this question
























  • 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 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 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 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 at 3:53

















up vote
0
down vote

favorite












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?










share|improve this question
























  • 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 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 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 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 at 3:53















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question















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?







bash shell-script command-substitution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 3 at 4:22

























asked Dec 3 at 3:05









abiyi

116




116












  • 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 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 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 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 at 3:53




















  • 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 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 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 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 at 3:53


















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 at 3:22




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 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 at 3:27




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 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 at 3:31




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 at 3:31




1




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 at 3:53






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 at 3:53












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485599%2ffind-in-command-substitution-empty-result-inside-while-result-in-command-line%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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.






    share|improve this answer

























      up vote
      0
      down vote













      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.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 3 at 4:04









        abiyi

        116




        116






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485599%2ffind-in-command-substitution-empty-result-inside-while-result-in-command-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre