Often, there arises a need to check if a particular file exists and/or
is it empty ?, this usually is the scenario when developing shell
scripts to automate stuff and a file is created by the script to hold
intermediate results. So when the script is scheduled to run next time,
it has to execute a sequence of commands if the file exists or it would
execute a different set of commands when the file is no longer
available.
Here is a simple code snippet which checks for the existence of a file and that it is NOT empty to return TRUE and it returns FALSE otherwise.
For different results to appear, try creating a file 'test_file' and by changing its contents to make it a sized file and/or to make it an empty file.
Here is a simple code snippet which checks for the existence of a file and that it is NOT empty to return TRUE and it returns FALSE otherwise.
if [ -s test_file ] ### if the test_file exists and that it is not empty ### i.e., file has contents then echo "TRUE" ### returns 'true' ONLY if the file exists and it is NOT empty, ### i.e., file has contents - file is not of 0 bytes in size else echo "FALSE" ### returns 'false' if the file exists and it is empty ### (0 bytes in size). ### returns 'false' also if the file does not exist fi
For different results to appear, try creating a file 'test_file' and by changing its contents to make it a sized file and/or to make it an empty file.
No comments:
Post a Comment