The logical operators for Shell script programming

Source: Internet
Author: User
Tags logical operators readable

1. Logical operators;

Logical operator Instances:

A=Tenb= -if[$a! =$b]then Echo"$a! = $b: A! = b"ElseEcho"$a! = $b: a = b"fiif[$a-lt --A $b-GT the]then Echo"$a less than 100 and $b greater than 15: Returns True"ElseEcho"$a less than 100 and $b greater than 15: returns false"fiif[$a-lt --O $b-gt -]then Echo"$a less than 100 or $b greater than 100: Returns True"ElseEcho"$a less than 100 or $b greater than 100: returns false"fiif[$a-lt5-O $b-gt -]then Echo"$a less than 5 or $b greater than 100: Returns True"ElseEcho"$a less than 5 or $b greater than 100: returns false"fi

Result of Operation:

2. String operators:

Example of a string operator:

A="ABC"b="EFG"if[$a =$b]then Echo"$a = $b: a equals b"ElseEcho"$a = $b: A is not equal to B"fiif[$a! =$b]then Echo"$a! = $b: A is not equal to B"ElseEcho"$a! = $b: a equals b"fiif[ -Z $a]then echo"-Z $a: string length is 0"ElseEcho"-Z $a: string length is not 0"fiif[ -n $a]then Echo"-N $a: string length is not 0"ElseEcho"-N $a: string length is 0"fiif[$a]then echo"$a: string is not empty"ElseEcho"string is empty"fi

Operation Result:

3. File Test operators:

File test Operator instance:

file="/home/chunyu/ursalinkshell/test.sh"if[ -r $file]then Echo"file readable"ElseEcho"file is not readable"fiif[ -w $file]then Echo"file can be written"ElseEcho"file is not writable"fiif[ -x $file]then echo"File Executable"ElseEcho"file is not executable"fiif[ -f $file]then Echo"file is a normal file"ElseEcho"files are special files"fiif[ -d $file]then Echo"file is a directory"ElseEcho"file is not a directory"fiif[ -s $file]then Echo"the file is not empty"ElseEcho"file is empty"fiif[ -e $file]then Echo"File exists"ElseEcho"file does not exist"fi

Result of Operation:

The logical operators for Shell script programming

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.