Linux exit file test test conditions

Source: Internet
Author: User

Exit 1 to end execution in the middle of the script

Cases:

[email protected] script]# cat usertest.sh
#!/bin/bash
#
Testuser=hbase
if! grep "^ $TESTUSER"/etc/passwd &>/dev/null; Then
echo "Not find User: $TESTUSER"
Exit 1
Fi
Useruid= ' grep ' ^ $TESTUSER "/etc/passwd | cut-d:-f3 '
Usergroupid= ' grep ' ^ $TESTUSER "/etc/passwd | cut-d:-f4 '
If [$USERUID-eq $USERGROUPID]; Then
echo "Good guy!"
Else
echo "Bed guy"
Fi
[Email protected] script]#

The first half of this script will execute exit 1 if the user is unable to find it. 0 The return value is executed for the correct state, and 1-255 is an error here, either one is OK. The rest does not explain hehe.


Common Test conditions for bash

Integer test

-GT Greater than

-lt less than

-ge greater than or equal to

-le less than or equal to

-ne Not equal to

-eq equals


Test mode 1. [Expression] 2. [[Expression]] 3. Test expression

The first two most commonly used


File test

-e file Test files exist

-F file Test files are normal files (non-linked files, device files, pipeline files)

-D path Tests whether the specified path is a directory

-R file tests whether the current user has read access to the specified file

-W file tests whether the current user has write access to the specified file

-X file tests whether the current user has execute permissions on the specified file

Cases:

[-e/etc/inittab] means testing whether the/etc/inittab file exists.

Linux exit file test test conditions

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.