Often you will want to do some file tests on the file system you are running. In this case, shell will provide you with several useful commands to achieve it.
The command looks like the following
[filename1] -<command> [filename2]
We will briefly introduce some common commands you might encounter in your daily life.
use "-e" to test if file exist
if [ -e "$filename" ]; then
echo "$filename exists as a file"
use "-d" to test if directory exists
if [ -d "$directory_name" ]; then
echo "$directory_name exists as a directory"
use "-r" to test if file has read permission for the user running the script/test
if [ ! -f "$filename" ]; then
if [ -r "$filename" ]; then
echo "you are allowed to read $filename"
echo "you are not allowed to read $filename"
There is no exercise for this section.