Shell Script--File test

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.