Bash script not recognizing file












0















I am attempting to write a bash script. In a test, I wrote a script to check for the existence of test.txt. However, no matter how many times I try to change the formatting, the code still does not recognize the file.



    while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done


I am 100% positive text.txt exists and is in the same folder as my script.










share|improve this question

























  • Are you sure test.txt actually exists?

    – that other guy
    Nov 25 '18 at 20:05








  • 3





    And don't just answer "yes" or "no" to the question above; show us the command you have used to verify the file existence.

    – mustaccio
    Nov 25 '18 at 20:06






  • 2





    You should read the input after you print the message.

    – Barmar
    Nov 25 '18 at 20:08






  • 1





    What ist your current directory when you start your script and in which directory is your file test.txt?

    – Cyrus
    Nov 25 '18 at 20:37








  • 3





    "In the same folder as my script" is not the folder that counts. The current directory when your script is running is the directory from which the script was started, not the directory the script is in.

    – Charles Duffy
    Nov 25 '18 at 20:52


















0















I am attempting to write a bash script. In a test, I wrote a script to check for the existence of test.txt. However, no matter how many times I try to change the formatting, the code still does not recognize the file.



    while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done


I am 100% positive text.txt exists and is in the same folder as my script.










share|improve this question

























  • Are you sure test.txt actually exists?

    – that other guy
    Nov 25 '18 at 20:05








  • 3





    And don't just answer "yes" or "no" to the question above; show us the command you have used to verify the file existence.

    – mustaccio
    Nov 25 '18 at 20:06






  • 2





    You should read the input after you print the message.

    – Barmar
    Nov 25 '18 at 20:08






  • 1





    What ist your current directory when you start your script and in which directory is your file test.txt?

    – Cyrus
    Nov 25 '18 at 20:37








  • 3





    "In the same folder as my script" is not the folder that counts. The current directory when your script is running is the directory from which the script was started, not the directory the script is in.

    – Charles Duffy
    Nov 25 '18 at 20:52
















0












0








0








I am attempting to write a bash script. In a test, I wrote a script to check for the existence of test.txt. However, no matter how many times I try to change the formatting, the code still does not recognize the file.



    while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done


I am 100% positive text.txt exists and is in the same folder as my script.










share|improve this question
















I am attempting to write a bash script. In a test, I wrote a script to check for the existence of test.txt. However, no matter how many times I try to change the formatting, the code still does not recognize the file.



    while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done


I am 100% positive text.txt exists and is in the same folder as my script.







bash shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 20:17







MAO3J1m0Op

















asked Nov 25 '18 at 20:03









MAO3J1m0OpMAO3J1m0Op

135




135













  • Are you sure test.txt actually exists?

    – that other guy
    Nov 25 '18 at 20:05








  • 3





    And don't just answer "yes" or "no" to the question above; show us the command you have used to verify the file existence.

    – mustaccio
    Nov 25 '18 at 20:06






  • 2





    You should read the input after you print the message.

    – Barmar
    Nov 25 '18 at 20:08






  • 1





    What ist your current directory when you start your script and in which directory is your file test.txt?

    – Cyrus
    Nov 25 '18 at 20:37








  • 3





    "In the same folder as my script" is not the folder that counts. The current directory when your script is running is the directory from which the script was started, not the directory the script is in.

    – Charles Duffy
    Nov 25 '18 at 20:52





















  • Are you sure test.txt actually exists?

    – that other guy
    Nov 25 '18 at 20:05








  • 3





    And don't just answer "yes" or "no" to the question above; show us the command you have used to verify the file existence.

    – mustaccio
    Nov 25 '18 at 20:06






  • 2





    You should read the input after you print the message.

    – Barmar
    Nov 25 '18 at 20:08






  • 1





    What ist your current directory when you start your script and in which directory is your file test.txt?

    – Cyrus
    Nov 25 '18 at 20:37








  • 3





    "In the same folder as my script" is not the folder that counts. The current directory when your script is running is the directory from which the script was started, not the directory the script is in.

    – Charles Duffy
    Nov 25 '18 at 20:52



















Are you sure test.txt actually exists?

– that other guy
Nov 25 '18 at 20:05







Are you sure test.txt actually exists?

– that other guy
Nov 25 '18 at 20:05






3




3





And don't just answer "yes" or "no" to the question above; show us the command you have used to verify the file existence.

– mustaccio
Nov 25 '18 at 20:06





And don't just answer "yes" or "no" to the question above; show us the command you have used to verify the file existence.

– mustaccio
Nov 25 '18 at 20:06




2




2





You should read the input after you print the message.

– Barmar
Nov 25 '18 at 20:08





You should read the input after you print the message.

– Barmar
Nov 25 '18 at 20:08




1




1





What ist your current directory when you start your script and in which directory is your file test.txt?

– Cyrus
Nov 25 '18 at 20:37







What ist your current directory when you start your script and in which directory is your file test.txt?

– Cyrus
Nov 25 '18 at 20:37






3




3





"In the same folder as my script" is not the folder that counts. The current directory when your script is running is the directory from which the script was started, not the directory the script is in.

– Charles Duffy
Nov 25 '18 at 20:52







"In the same folder as my script" is not the folder that counts. The current directory when your script is running is the directory from which the script was started, not the directory the script is in.

– Charles Duffy
Nov 25 '18 at 20:52














1 Answer
1






active

oldest

votes


















2














in this case I would add some debug lines to my script to make sure my thinking is correct. For instance:



while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
echo "debug: now I'm in $( pwd ) directory. dir listing:"
ls -la
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done


also, please note that linux filenames are case sensitive (meaning you can have test.txt and TeSt.txt in the same directory). So, for instance if you have the file named TEST.TXT, [ -f test.txt ] will evaluate to false (unless test.txt exists as well)






share|improve this answer
























  • "$PWD" is much more efficient to run than $(pwd) (which spawns off a subshell, fork()ing off a whole new process just to check the directory in it).

    – Charles Duffy
    Nov 25 '18 at 20:48











  • Also, the [ -f $COMMANDFILE ] test is itself faulty due to the lack of quotes; if the current value of IFS contains a ., for example, it'll parse as [ -f "test" "txt" ], which is false.

    – Charles Duffy
    Nov 25 '18 at 20:49













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53471391%2fbash-script-not-recognizing-file%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









2














in this case I would add some debug lines to my script to make sure my thinking is correct. For instance:



while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
echo "debug: now I'm in $( pwd ) directory. dir listing:"
ls -la
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done


also, please note that linux filenames are case sensitive (meaning you can have test.txt and TeSt.txt in the same directory). So, for instance if you have the file named TEST.TXT, [ -f test.txt ] will evaluate to false (unless test.txt exists as well)






share|improve this answer
























  • "$PWD" is much more efficient to run than $(pwd) (which spawns off a subshell, fork()ing off a whole new process just to check the directory in it).

    – Charles Duffy
    Nov 25 '18 at 20:48











  • Also, the [ -f $COMMANDFILE ] test is itself faulty due to the lack of quotes; if the current value of IFS contains a ., for example, it'll parse as [ -f "test" "txt" ], which is false.

    – Charles Duffy
    Nov 25 '18 at 20:49


















2














in this case I would add some debug lines to my script to make sure my thinking is correct. For instance:



while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
echo "debug: now I'm in $( pwd ) directory. dir listing:"
ls -la
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done


also, please note that linux filenames are case sensitive (meaning you can have test.txt and TeSt.txt in the same directory). So, for instance if you have the file named TEST.TXT, [ -f test.txt ] will evaluate to false (unless test.txt exists as well)






share|improve this answer
























  • "$PWD" is much more efficient to run than $(pwd) (which spawns off a subshell, fork()ing off a whole new process just to check the directory in it).

    – Charles Duffy
    Nov 25 '18 at 20:48











  • Also, the [ -f $COMMANDFILE ] test is itself faulty due to the lack of quotes; if the current value of IFS contains a ., for example, it'll parse as [ -f "test" "txt" ], which is false.

    – Charles Duffy
    Nov 25 '18 at 20:49
















2












2








2







in this case I would add some debug lines to my script to make sure my thinking is correct. For instance:



while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
echo "debug: now I'm in $( pwd ) directory. dir listing:"
ls -la
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done


also, please note that linux filenames are case sensitive (meaning you can have test.txt and TeSt.txt in the same directory). So, for instance if you have the file named TEST.TXT, [ -f test.txt ] will evaluate to false (unless test.txt exists as well)






share|improve this answer













in this case I would add some debug lines to my script to make sure my thinking is correct. For instance:



while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
echo "debug: now I'm in $( pwd ) directory. dir listing:"
ls -la
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done


also, please note that linux filenames are case sensitive (meaning you can have test.txt and TeSt.txt in the same directory). So, for instance if you have the file named TEST.TXT, [ -f test.txt ] will evaluate to false (unless test.txt exists as well)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 20:46









ImreImre

866




866













  • "$PWD" is much more efficient to run than $(pwd) (which spawns off a subshell, fork()ing off a whole new process just to check the directory in it).

    – Charles Duffy
    Nov 25 '18 at 20:48











  • Also, the [ -f $COMMANDFILE ] test is itself faulty due to the lack of quotes; if the current value of IFS contains a ., for example, it'll parse as [ -f "test" "txt" ], which is false.

    – Charles Duffy
    Nov 25 '18 at 20:49





















  • "$PWD" is much more efficient to run than $(pwd) (which spawns off a subshell, fork()ing off a whole new process just to check the directory in it).

    – Charles Duffy
    Nov 25 '18 at 20:48











  • Also, the [ -f $COMMANDFILE ] test is itself faulty due to the lack of quotes; if the current value of IFS contains a ., for example, it'll parse as [ -f "test" "txt" ], which is false.

    – Charles Duffy
    Nov 25 '18 at 20:49



















"$PWD" is much more efficient to run than $(pwd) (which spawns off a subshell, fork()ing off a whole new process just to check the directory in it).

– Charles Duffy
Nov 25 '18 at 20:48





"$PWD" is much more efficient to run than $(pwd) (which spawns off a subshell, fork()ing off a whole new process just to check the directory in it).

– Charles Duffy
Nov 25 '18 at 20:48













Also, the [ -f $COMMANDFILE ] test is itself faulty due to the lack of quotes; if the current value of IFS contains a ., for example, it'll parse as [ -f "test" "txt" ], which is false.

– Charles Duffy
Nov 25 '18 at 20:49







Also, the [ -f $COMMANDFILE ] test is itself faulty due to the lack of quotes; if the current value of IFS contains a ., for example, it'll parse as [ -f "test" "txt" ], which is false.

– Charles Duffy
Nov 25 '18 at 20:49






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f53471391%2fbash-script-not-recognizing-file%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

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft