6 | if statements

Shell Scripting Tutorial


Hello Programmers, I’m Yash. Sole programmer at planetvearcity – a place where programmers come to learn, build, and grow.

Welcome to the tutorial series on shell scripting. In this video, we will learn how to use a file condition operator along with if statement. There will be times when you will be dealing with files in your scripts. These operators make it easier for us to check conditions such as is file regular file, file exist or not, and much more.

– – – – File Test Opeartors – – – – –

The ones we will test:

a : True if the file exists.
e : True if the file exists.
d : True if the file exists and is a directory.
f : True if the file exists and is a regular file.
s : True if the file exists and has a size greater than zero.
r : True if the file exists and is readable.
w : True if the file exists and is writable.
x : True if the file exists and is executable.

The ones we will not test:

b : True if the file exists and is a block special file.
c : True if the file exists and is a character special file.
g : True if the file exists and its SGID bit is set.
h : True if the file exists and is a symbolic link.
k : True if the file exists and its sticky bit is set.
p : True if the file exists and is a named pipe (FIFO).
t : True if file descriptor is open and refers to a terminal.
u : True if the file exists and its SUID (set user ID) bit is set.
O : True if the file exists and is owned by the effective user ID.
G : True if the file exists and is owned by the effective group ID.
L : True if the file exists and is a symbolic link.
N : True if the file exists and has been modified since it was last read.
S : True if the file exists and is a socket.