OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

File Expressions

The following expressions are used to evaluate the status of files:


Table 27-1: test File Expressions


Expression Is True If:

Expression Is True If:

file1 -ef file2 file1 and file2 have the same inode numbers (the two

filenames refer to the same file by hard linking).


image

file1 -nt file2 file1 is newer than file2.


image

file1 -ot file2 file1 is older than file2.


image

-b file file exists and is a block-special (device) file.


image

-c file file exists and is a character-special (device) file.


image

-d file file exists and is a directory.


image

-e file file exists.


image

-f file file exists and is a regular file.


image

-g file file exists and is set-group-ID.


image

-G file file exists and is owned by the effective group ID.


image

-k file file exists and has its “sticky bit” set.


image


image

-L file file exists and is a symbolic link.


image

-O file file exists and is owned by the effective user ID.


image

-p file file exists and is a named pipe.


image

-r file file exists and is readable (has readable permission for the effective user).


image

-s file file exists and has a length greater than zero.


image

-S file file exists and is a network socket.


image

-t fd fd is a file descriptor directed to/from the terminal. This can be used to determine whether standard input/output/error is being redirected.


image

-u file file exists and is setuid.


image

-w file file exists and is writable (has write permission for the effective user).


image

-x file file exists and is executable (has execute/search permission for the effective user).


image


Here we have a script that demonstrates some of the file expressions:



#!/bin/bash


# test-file: Evaluate the status of a file FILE=~/.bashrc

if [ -e "$FILE" ]; then

if [ -f "$FILE" ]; then

echo "$FILE is a regular file."

fi

if [ -d "$FILE" ]; then

echo "$FILE is a directory."

fi

if [ -r "$FILE" ]; then

echo "$FILE is readable."

fi

if [ -w "$FILE" ]; then

echo "$FILE is writable."

fi

if [ -x "$FILE" ]; then

echo "$FILE is executable/searchable."

#!/bin/bash


# test-file: Evaluate the status of a file FILE=~/.bashrc

if [ -e "$FILE" ]; then

if [ -f "$FILE" ]; then

echo "$FILE is a regular file."

fi

if [ -d "$FILE" ]; then

echo "$FILE is a directory."

fi

if [ -r "$FILE" ]; then

echo "$FILE is readable."

fi

if [ -w "$FILE" ]; then

echo "$FILE is writable."

fi

if [ -x "$FILE" ]; then

echo "$FILE is executable/searchable."


fi else

echo "$FILE does not exist" exit 1

fi


exit

fi else

echo "$FILE does not exist" exit 1

fi


exit


The script evaluates the file assigned to the constant FILE and displays its results as the evaluation is performed. There are two interesting things to note about this script. First, notice how the parameter $FILE is quoted within the expressions. This is not required to syntacticly complete the expression, rather it is a defense against the parameter being empty. If the parameter expansion of $FILE were to result in an empty value, it would cause an error (the operators would be interpreted as non-null strings rather than opera- tors). Using the quotes around the parameter ensures that the operator is always followed by a string, even if the string is empty. Second, notice the presence of the exit com- mand near the end of the script. The exit command accepts a single, optional argument, which becomes the script’s exit status. When no argument is passed, the exit status de- faults to the exit status of the last command executed. Using exit in this way allows the script to indicate failure if $FILE expands to the name of a nonexistent file. The exit command appearing on the last line of the script is there as a formality. When a script “runs off the end” (reaches end of file), it terminates with an exit status of the last com- mand executed.

Similarly, shell functions can return an exit status by including an integer argument to the return command. If we were to convert the script above to a shell function to include it in a larger program, we could replace the exit commands with return statements and get the desired behavior:



test_file () {


# test-file: Evaluate the status of a file FILE=~/.bashrc

if [ -e "$FILE" ]; then

if [ -f "$FILE" ]; then

echo "$FILE is a regular file."

fi

if [ -d "$FILE" ]; then

echo "$FILE is a directory."

fi

if [ -r "$FILE" ]; then

test_file () {


# test-file: Evaluate the status of a file FILE=~/.bashrc

if [ -e "$FILE" ]; then

if [ -f "$FILE" ]; then

echo "$FILE is a regular file."

fi

if [ -d "$FILE" ]; then

echo "$FILE is a directory."

fi

if [ -r "$FILE" ]; then


echo "$FILE is readable."

fi

if [ -w "$FILE" ]; then

echo "$FILE is writable."

fi

if [ -x "$FILE" ]; then

echo "$FILE is executable/searchable."

fi

else

echo "$FILE does not exist" return 1

fi


}

echo "$FILE is readable."

fi

if [ -w "$FILE" ]; then

echo "$FILE is writable."

fi

if [ -x "$FILE" ]; then

echo "$FILE is executable/searchable."

fi

else

echo "$FILE does not exist" return 1

fi


}


Top OS Cloud Computing at OnWorks: