| by Arround The Web | No comments

Bash if –e and –s and other File Test Operators

In Linux files, test operators are used for checking the attributes associated with a file like: the author of the file, administrative rights, and ownership of the file, and to check whether they are symlink or not. When we are dealing with files, it becomes necessary to check their existence and whether they contain any content or not for this purpose; or if a statement is used along with the test operators.

File test operators in bash are also used to test the files as the name explains. With the help of these operators, we can test different aspects of a file like if that file is writable or readable only. There are many file test operators and each one has a different role and use. File test operators help us to prevent errors while scripting in Bash. To perform that task, we use an if-else statement. The if-else statement acts as a very useful tool for error handling in Bash scripting.

The purpose of the “-e” operator is to tell us whether a file exists in the specified directory or not. The “-s” operator tells us that the file is not a zero size which means that this operator tells us whether the file in the specified directory contains any data or not. Other operators are used to dealing with files but the “-e” and “-s” are the most important ones and are frequently used.

How to Use Test Operators:

In this illustration, we will try to use all of the test operators to perform various tasks on the files.

Example no. 1: Using the if –e Text Operator

In this example, we will try to use the if –e test operator which is mainly used to verify whether the file exists in a directory or not. Now, we will first create a new file.

linux@linux-Virtualbox:~$ nano testoperator.sh

When we run the command above, it will open the empty file now we will write the code in which we will create a condition that the file “employee.txt” exist it will display the echo command which contains the message “File exists”. Else, it will execute the message “File does not exist”. The file testoperator will contain the content mentioned below.

#!/bin/sh
file="employee.txt"
if [ -e $file ]
then
   echo "File exists"
else
   echo "The file does not exist"
fi

After saving the file using the bash command, we will run the below-mentioned command which will execute the testoperator.sh file.

linux@linux-Virtualbox:~$ bash testoperator.sh

When we run the above command, it will read the code of the testoperator file which executes and check for the file in the home directory. In this case, we do not have any file in the directory, so it executed the output below.

Output:

The file does not exist

Now, we will first create a new file named “employee.txt”. Then, again run the command to check whether it will detect the file or not. As shown below, this time the file exists in the home directory.

Now, let us run the above command again. This time, it displays the output that is shown below in which the message “File exists”.

Output:

File exists

Example no. 2: Using the if –d Text Operator

In this example, we will try another test operator that is the “-d” operator which is used to verify whether the file we are checking is a directory or not. This time, we will create a new directory which we named “employee”, as can be seen in the snippet below.

Now, we will modify the “testoperator.sh” file in which we will change the file name “employee.txt” to just “employee”. Then, passing the “-d” operator in the if statement along with the “$file” which will be fetched out. And changing the message, that will be displayed as output, we will save the file. Now, the “testoperator.sh” file contains the following content.

#!/bin/sh
file="employee”
if [ -d $file ] then
   echo "
The employee is a directory"
else
   echo "
The employee is not a directory "
fi

After modifying the “testoperator.sh”, we will run the bash command along with the file name :

linux@linux-Virtualbox:~$ bash testoperator.sh

When the command above executes, it will display the output shown below in which it prints the message that is:

Output:

The employee is a directory

Example no. 3: Using the if –s Text Operator

In this example, we will try to use the “-s” operator which is used to check whether the file contains any content or not. For that, we will be using the already created file that we used in example 1 named “employee.txt”. Let us first modify the file testoperator.sh in which we will pass the “-s” option along with the file name to our if statement and assign the file “employee.txt”. Also, it will change the message that will be displayed as an output.

#!/bin/sh
file="employee.txt”
if [ -d $file ] then
   echo "
employee.txt is empty"
else
   echo "
employee.txt is not empty"
fi

Now, we will save and close the file “testoperator.sh” and run the command mentioned below in the terminal.

linux@linux-Virtualbox:~$ bash testoperator.sh

When the command above runs, it will execute the else statement because the employee.txt contains the content. So, it ignores the then statement echo command.

Output:

employee.txt is not empty

Now, we will remove the content from the “employee.txt” and then run the command again. When the command above is executed once again, it displays the then statements message because it does not contain any content in it. The example below is the output of the command that we executed after deleting the content of the file.

Output:

employee.txt is empty

Example no. 4: Using the if –r Text Operator

In this example, we will implement another test operator that is “-r”. This operator is used to check whether the file is readable to the user or not. For this, we will modify the testoperator.txt file by replacing the “-d” operator with “-r”. It will also change the output message which is if the file is readable. It will display the message “employee.txt is readable and if the file is not readable it will display the else statement. Now, the “testoperator.txt” contains the following content:

#!/bin/sh
file=”employee.txt”
if [ -r $file ]
then
   echo “employee.txt is readable”
else
   echo “ employee.txt is readable “
fi

Now, we will run the command below to execute the code written inside it.

linux@linux-Virtualbox:~$ bash testoperator.sh

After running the above command, the displayed output below will be printed on the terminal.

Output:

employee.txt is readable

Conclusion

We have discussed the most important part of the filing system, the test operators. While working with files, the test operators are the most essential utility provided by Linux packages. After introducing the purpose of the test operators, we explained them by implementing these operators in various examples.

Share Button

Source: linuxhint.com

Leave a Reply