When writing scripts in Linux, there is always the need to determine whether the existence of files, the contents of the file is empty and so on, and these operations can be implemented with the test instructions, through the man test instructions can be viewed on the test instructions manual, the manual has the following description:
-S FILE
FILE exists and has a size greater than zero
If the file exists and the file size is greater than 0, return the true
-E FILE
FILE exists
If the file exists, return the true
The sample script that tests whether the file is empty through the test instruction in the shell is as follows:
#! /bin/sh
If test-s file.txt then
echo "HI"
else
echo "Empty"
fi
In the shell, the test instruction has another way of writing, and the above script is equivalent to the following script:
#! /bin/sh
If [-s file.txt]; then
echo "HI"
else
echo "Empty"
fi
The difference between the two people to observe their own slowly AH ~ ~
Reprint Please specify: Coding ants» How to determine whether a file is empty in the shell