test can be used to if statements) -a | and |
-o | or |
-ef | Same device and inode number |
-ot | Left hand file is older than right hand file |
(…) | |
! | not |
= | String equality |
-eq | Integer equality |
!= | String inequality |
-ge | Greater or equal (integer) |
-gt | Greater than (integer) |
-le | Less or equal (integer) |
-lt | Less than (integer) |
-z | Length (of string) is zero |
-n | Length (of string) is not zero |
-l | Evaluates to the length of the given string |
-b | block special |
-c | character special |
-d | directory |
-e | file exists |
-f | regular file |
-g | set-group-ID |
-G | owned by the effective group ID |
-h | symbolic link, same as -L |
-k | has its sticky bit set |
-L | symbolic link, same as -h |
-N | has been modified since it was last read |
-O | owned by the effective user ID |
-p | named pipe |
-r | read permission is granted |
-s | has a size greater than zero |
-S | is a socket |
-t | Argument is an integer that represents an opened file descriptor on a terminal |
-u | its set-user-ID bit is set |
-w | write permission is granted |
-x | execute (or search) permission is granted |
-h and -L, all file-related tests dereference symbolic links. #!/bin/sh
#
# TODO: -G, -L, -N, -O, -S, -ef, -nt, -ot, -o, -v, -R, -z, -n, ==, =, !=, <, >, -eq, -ne, -lt, -le, -gt, -ge
#
test_file() {
echo
echo testing $1
if [ -a $1 ]; then
echo " File exists"
else
echo " File does not exist"
fi
if [ -b $1 ]; then
echo " Block special file"
else
echo " No block special file"
fi
if [ -c $1 ]; then
echo " Character special file"
else
echo " No character special file"
fi
if [ -d $1 ]; then
echo " Is a directory"
else
echo " Is not a directory"
fi
if [ -e $1 ]; then
echo " File exists"
else
echo " File does not exist"
fi
if [ -f $1 ]; then
echo " Regular file"
else
echo " No regular file"
fi
if [ -g $1 ]; then
echo " set-group-id bit is set"
else
echo " set-group-id bit is not set"
fi
if [ -h $1 ]; then
echo " File is symbolic link"
else
echo " File is not a symbolic link"
fi
if [ -k $1 ]; then
echo " Sticky bit is set"
else
echo " Sticky bit not set"
fi
if [ -p $1 ]; then
echo " Named pipe (FIFO)"
else
echo " No named pipe (FIFO)"
fi
if [ -r $1 ]; then
echo " File is readable"
else
echo " File is not readable"
fi
if [ -s $1 ]; then
echo " File size > 0"
else
echo " File size = 0 or file does not exist"
fi
if [ -t $1 ]; then
echo " File descriptor open and refers to terminal"
else
echo " File descriptor not open or not referring to terminal"
fi
if [ -u $1 ]; then
echo " set-user-id bit is set"
else
echo " set-user-id bit is not set"
fi
if [ -w $1 ]; then
echo " File is writable"
else
echo " File is not writeable"
fi
if [ -x $1 ]; then
echo " File is executable"
else
echo " File is not executable"
fi
}
test_file Conditional-Expressions
test_file $1