testing for the existence of a file
In Ubuntu, the test
command can be used to test for the existence of a file. For example, the following script uses test
and an if-construct to test for the existence of a file named foo.bar:
FNAME="foo.bar" ; if test ! -s "$FNAME" ; then echo "$FNAME does not exist or is empty." ; else echo "$FNAME exists." ; fi
explanation
Note
This is an incomplete explanation.
- The
test ! -s "$FNAME"
command tests for the existence of a file with a name stored in the$FNAME
variable, returning true if the filename exists. This directs the script's control flow to either show a message indicating that the file does not exist or is empty, or show a message indicating that the file exists.
prior work
- The
test
command was introduced to me by a comment on a question I asked on Stack Overflow by fmw42. - The bulk of the contents of the script's if-construct was introduced to me by the test (Unix)§Examples page on Wikipedia.
- The correct syntax for the if-contruct was introduced to me by an answer on Stack Overflow by Shawn Chin.
licensing
Some rights reserved: CC BY-SA 3.0. Includes significant content from test (Unix)§Examples on Wikipedia, with changes made.