Leeboy's Linux learning fifteen shell conditional testing and simple conditional statement instances

Source: Internet
Author: User

1,Echo $?: An exit status is returned when any command is running, and the exit status of the previous command is output,0Indicates that the exit is successful.

2, Test File status:[-W text]Test whether the file can be written,Echo $?The output is0Indicates writable; otherwise, it cannot be written.

[-D text]Is the file a directory,Echo $?The output is1Indicates that it is not a directory.0Indicates conformity,1Indicates non-conformity.

[-W text-a-d text]Is the file writable and path,-Indicates the sameAnd,-OIndicatesOr

-DDirectory-SThe file length is greater0, Not empty

-FRegular File-WWritable

-LSymbolic connection-UFile hasS u I dBit settings

-RReadable-XExecutable

3, Test string:[String string_operator string]: For example[$ Leeboy = "hello"],[-Z
$ Leeboy].

=The two strings are equal .!=Two strings.-ZEmpty string.-NNon-empty string.

4, Test value:[Number number_operator number]: For example[$ Leeboy-EQ 9]

-EQThe value is equal.

-NeThe values are not equal.

-GTThe first number is greater than the second number.

-LtThe first number is smaller than the second number.

-LeThe first number is less than or equal to the second number.

-GeThe first number is greater than or equal to the second number.

5,IfIn the script.

 
#! /Bin/sh # Check whether the input is empty echo-e "enter your name: \ c" read nameif ["$ name" = ""] # Double quotation marks must be used here, otherwise, thenecho "You did not enter your name! "Elif [" $ name "=" leeboy "]; then # If then is written in a row, add"; "Echo" you are the one! "Elseecho" Hello $ name! "Fi


6Input parameters for the script

#! /Bin/sh # determine the number of input parameters if [$ #-lt 3] # $ # is the number of parameters thenecho "Usage: 'basename $0 ': arg1 arg2 arg3 "Exit 1 fiecho" basename: $0 "# $0 indicates the shell script name echo" arg1: $1 "# shell parameters echo" arg2: $2 "Echo" arg3: $3"

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.