A file test is a test of whether a file or directory exists
Test file format [operator directory or file] Note that there is a space between the opening parenthesis and the operator, and there is a space between the file or directory and the parenthesis on the right.
-D |
Whether the test is a directory |
-E |
Test whether a file or directory exists |
-F |
Test whether the file |
-R |
To test whether the current user has read access to the file |
-W |
To test whether the current user has write access to the file |
-X |
To test whether the current user has execute permissions on the file |
-L |
Test if the file is a symbolic link |
The results of the test can be obtained by using $, $? Indicates whether the execution of the previous command succeeded or judged whether the condition was true: if the run succeeds, 0 is returned, and a non-0 value is returned if an exception or failure occurs. So you can make a $? To determine if the file test was successful.
Instance:
#!/bin/bash# file name: test.sh[-d/etc]echo $? [-f/etc]echo $?] [-F test.sh]echo $? [-X test.sh]echo $?
Operation Result:
Strengthen:
#!/bin/bash# file name: test.sh[-d/etc] && echo "One" [-f/etc] && echo "One" [-F test.sh] && echo "t Hree "[x test.sh] && echo" Four "
Run:
Shell Script--File test