Introduction
Let's ensure we understand the foundational concepts before delving further into the subjects. Here is a brief introduction if you are unfamiliar with the Shell script.
A shell script is a short piece of software designed to be run by the Unix shell, a command-line interpreter. A shell script is essentially a set of instructions that the shell in an operating system based on Unix follows.
In this blog, we will discuss a few operators for testing different properties in shell scripting.
Without further ado, let's get started.
Shell Scripting - File test operators
💁 To examine different aspects of a Unix file's features, we have a few operators.
Assume a variable file contains the existing file "ninja.sh," which has the following permissions: read, write, and execute.
Operator |
Description |
How to use |
-b file | Check to see if the file is a block special file; if yes, the condition is met. | [ -b $file ] |
-c file | Check to see if the file is a character special file; if yes, the condition is met. | [ -c $file ] |
-d file | Check to see if the file is a directory; if yes, the condition is met. | [ -d $file ] |
-f file | The condition is true if the file is an ordinary file as opposed to a directory or special file. | [ -f $file ] |
-g file | Check to see if the file's set group ID (SGID) bit is set; if yes, the condition is met. | [ -g $file ] |
-k file | Check to see if the file's sticky bit is set; if it is, the condition is met. | [ -k $file ] |
-p file | Check to see if this is a named pipe; if yes, the condition is met. | [ -p $file ] |
-t file | The condition is true if the file descriptor is open and linked to a terminal. | [ -t $file ] |
-u file | The condition is true if the file's Set User ID (SUID) bit is set. | [ -u $file ] |
-r file | Checks to see if the file can be read; if yes, the condition is met. | [ -r $file ] |
-w file | Checks to see if the file can be written; if yes, the condition is met. | [ -w $file ] |
-x file | Checks to see if the file can be executed; if yes, the condition is met. | [ -x $file ] |
-s file | Check to see if the file is larger than zero; if yes, the condition is met. | [ -s $file ] |
-e file | When a file is a directory but still exists, the existence check is true. | [ -e $file ] |
Example
Each of the file test operators is used in the example below.
Assume a variable file contains the existing file "ninja.sh," which has the given permissions: read, write, and execute.
#!/bin/sh
file="ninja.sh"
if [ -r $file ]
then
echo "The File has read access."
else
echo "File does not have read access."
fi
if [ -w $file ]
then
echo "The file has write access."
else
echo "There is no write permission for the file."
fi
if [ -x $file ]
then
echo "File has execute permission."
else
echo "There is no execute permission for the file."
fi
if [ -f $file ]
then
echo "File is an ordinary file."
else
echo "This is a special file."
fi
if [ -d $file ]
then
echo "File is a directory."
else
echo "This is not a directory."
fi
if [ -s $file ]
then
echo "File size is not zero."
else
echo "File size is zero."
fi
if [ -e $file ]
then
echo "File exists."
else
echo "File does not exist."
fi
Output
File does not have read access.
There is no write permission for the file.
There is no execute permission for the file.
This is a special file.
This is not a directory.
File size is not zero.
File does not exist.
Read about Bitwise Operators in C here.