MAO3J1m0Op
MAO3J1m0Op

Reputation: 421

Bash script not recognizing file

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.

Upvotes: 0

Views: 1792

Answers (1)

Imre
Imre

Reputation: 332

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)

Upvotes: 2

Related Questions