Shell Scripting Learning notes-logical operators

Source: Internet
Author: User
Tags logical operators true true

1.1 The following table is a logical operator

Tips:

! Inverse: A logical value that is opposite to a logical value.

-A Chinese meaning is with (&&): Two logical values are true return values are true, and vice versa is false.

-O Chinese meaning yes or (or): Two logical values if one is true, the return value is true.

Conclusion:

(1)-A and && operating rules: Only the two ends are 1 to be true equivalent to the multiplication operation.

True true 1 false false 0

and 1*0=0

and 0*1=0

and 1*1=1

and 0*0=0

Only the ends are 1 true, and the intersection of and

(2)-O or | | Both ends are 0 false, and no end of 0 is true

Or 1+0=1 really

Or 1+1=2 really

Or 0+1=1 really

Or 0+0=0 False

Both ends are 0 false, not 0 is true. Or Set

(3) Here is an example of a system not equal to

Example Demo:

1.2 Practice

(1) "-a" and "-O", the logical operation symbol is used in [].

(2) "&&" and "| |", the logical operation symbol is used in [[]].

(3) Note that there must be spaces at both ends of the brackets.

1.3 System Script Example

1.4 Summary: Summary of the use of logical operators

(1) [] with-a,-o,!

(2) [[]] with &&,| |,!

(3) Test usage is the same as [].

(4) between multiple [] and multiple [[]], or any of the mixed intermediate logical operators are && or | |.

1.5 General Examples:

(1) Compare the size of the two integers by defining variables, script arguments, and read-in in a conditional expression (prohibit using if) and alerting the user to the results in the form of a screen output. A total of 3 scripts developed, when using script to pass the parameter and read in the way you need to be a variable is a number, and not to give hints.

A. Script-pass mode

B.read read-in mode

Defining a variable is changing the value of a and B in read into an integer and then removing the read from here I don't have an example.

(2) Print Selection menu, install Web Service with one click

Requirements:

When the user enters 1 o'clock, the output "start installing lamp" then executes/server/scripts/lamp.sh, the script content output "lamp is installed" after exiting the script; When the user enters 2 o'clock, the output "start Installing LNMP "then executes/server/script/lnmp.sh, outputs" LNMP is installed "and exits the script when input 3 o'clock exits the current menu and script. When you enter any other character, give the hint "Input error" and exit the script. To determine the relevant conditions for the executed script, for example: whether the script exists, whether it can be executed, and so on.

Print menu to implement Web Service installation

[[email protected] ~]# cat menu.sh #!/bin/bashmenu(){cat <<EOF1.[ install lamp ]2.[ install lnmp ]3.[ exit ]please input the num you want:EOF}menuread num[ "$num" -eq "1" ]&&{[ -x read2.sh ]||{echo "read2.sh cat exec"exit 1}echo "start install lamp"sh ./read2.shecho "lamp is installed"exit 0}[ "$num" -eq "2" ]&&{[ -x ./2.sh ]||{echo "2.sh cat exec."exit 1}echo "start install lnmp"sh ./2.shecho "lnmp is install"exit 0}[ "$num" -eq "3" ]&&{exit 0}echo "input error"

Shell Scripting Learning notes-logical operators

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.