This simple example shows how to check if a file exists or not.
This is how we do this.
if [ -f "/home/jason/foo" ] |
Putting it together in a script makes it simple to verify if the file exists or not.
if [ -f "/home/jason/foo" ] then echo "file does exist." else echo "file not found." fi |
To check if a user exists on your machine, use this syntax.
if getent passwd jason > /dev/null 2>&1; then echo "yes the user exists" else echo "No, the user does not exist" fi |
This will check if the user “jason” exists or not.
This type of scripting is very useful if a file needs to be created, and you wish to check if it is already there.