I have the following code to detect if a file exist:
#!/bin/bash
VAR="/home/$USER/MyShellScripts/sample.txt"
if [ -e '$VAR' ]
        then
                echo "$VAR exist"
        else
                echo "the file $VAR doesn't exist!"
fi
if [ -x '$VAR' ]
        then
                echo "You have permissions to execute the file $VAR"
        else
                echo "You do not have permissions to execute the file $VAR"
fi
When I apply the ls command on the specified directory I see:
MyLAPTOP:~/MyShellScripts$ ls
exercise_4.sh  exercise_4Original.sh  first.sh  sample.txt  second.sh
So, if the file exists in the specified directory, why it is not detected? Below the script output:
MyLAPTOP:~/MyShellScripts$ ./exercise_4.sh
the file /home/username/MyShellScripts/sample.txt doesn't exist!
You do not have permissions to execute the file /home/username/MyShellScripts/sample.txt
 
     
     
    