Shell script Base numeric operation judgment and if statement

Source: Internet
Author: User
Tags yum repolist

Numeric operations

Integer operation "Three kinds, one can be mastered"
Expr numeric operator number
[[email protected] ~]# Expr 1 + 1 (OP symbols are all +-*/NOTE: * Requires \*.) % is the remainder, only 0 1 2)
2
[[email protected] ~]# Expr 45 \* 1
45

echo\$[numeric operator numbers]
[[email protected] ~]# echo \$[89*89+454848*874851]
397924235569

Let
i++ = = I=i+1
i+=2 = = I=i+2
i*=2 = = I=i*2
i\=2 = = I=i\2
I-=3 = = I=i-2
[Email protected] ~]# a=13
[[email protected] ~]# let a++
[Email protected] ~]# echo \$a
14
[[email protected] ~]# let a+=6
[Email protected] ~]# echo \$a
20

decimal operation "BC" (Can do logical operation, pipeline operation is convenient)
[Email protected] ~]# echo "84515*4511.894654+4856.844*154.158-15451" |BC
382056047.039810
[Email protected] ~]# echo "899<1" |BC
0
[Email protected] ~]# echo "899>1" |BC
1

————————————————————————————————————————
Combination of commands:

Syntax format
Judge:
[]: Judgment statement to be executed in parentheses

Logical-delimited operations
&&: A given condition must be established, the entire test result is true
|| : The entire test result is true as long as one of the conditions is set
;: Execute first, then execute

A&&b: Only a succeeds, only executes B
A B: Execute a, then execute B
a| | B: Only a failed to execute B

Common Test actions: (directory, file)
-E: Determines whether the object exists or is true
-D: Determines whether the object is a directory or True
-F: Determines whether the object is a generic file, or True
-R: Determines whether the object has a readable permission, is true
-W: Determines whether the object has writable permissions, is True
-X: Determines whether an object has executable permissions or is True
-S: Determine if the file is not empty,

Integer value comparison: (quantity)
-eq: Equals
-ne: Not equal to
-ge: greater than or equal to
-le: Less than or equal to
-GT: Greater Than
-LT: Less than

string comparison:
= =: two strings same
! =: Two strings are not the same
-Z: null value of String
-N: The value of the string is not empty, (equivalent! -Z)

[[email protected] ~]# Vim a.sh (delete script)
#!/bin/bash
Read-p "Crouching groove, you really want to delete ah y/n" sure (read-p display content on the terminal)
[-Z \$sure]&&exit (determine if \$sure is empty (-Z) &&: matches, stops for)
[\$sure = y]&& rm-rf \$ (if you enter Y && match, continue to the next step)
[Email protected] ~]#/a.sh ftp.sh
You're really going to remove it, y/ny.

[[email protected] ~]# vim b.sh (monitor login user volume)
#!/bin/bash
Num= ' Who |wc-l ' (' anti-apostrophe ' write command, this command counts how many users, put the number in num)
[\$num-ge 4]&& mail-s ' Errro ' root </etc/passwd (when the number of users is greater than (GE) 4 &&: compliant, execute send mail)
* * * * * */root/b.sh

[[email protected] ~]# vim a.sh (guessing word game)
#!/bin/bash
xx=\$ ((random%10)) (xx variable, the random number generated by the Linux system,%10 represents 10 of the number)
[\$cai-eq \$xx]&& echo OK &&exit| | Echo Kill (when the number entered equals (eq) shows OK and exits, not kill)

[Email protected] ~]# vim a.sh
#!/bin/bash
[!-d/mnt/iso]&& Mkdir/mnt/iso (! -D Inverse, if true &&: compliant, create Mnt/iso)
[-e/iso/rhel-server-6.7-x86_64-dvd.iso]&& Mount-o loop/iso/rhel-server-6.7-x86_64-dvd.iso/mnt
/iso (-E: Determines whether the object exists, &&: Conforms, exists on Mount to Mnt/iso)

————————————————————————————————————————————————————————

If statement
Single-branch format: (executes the following command as long as the judgment is met)
If [Judge];then
Command
.. ..
Fi
[Email protected] ~]# vim a.sh
#!/bin/bash
if [\$USER = root];then
Yum Clean All
Yum Repolist
Fi

Dual-branch format: (Judgment not satisfied, re-judge to perform the next condition)
If [Judge];then
Command
elif [Judging];then
Command
Else
Command
Fi
Test:
[Email protected] ~]# vim a.sh
#!/bin/bash
If [\$hydra-gt 9999];then
echo "Hail Hydra"
elif [\$hydra-gt 8888];then
echo "Hail"
elif [\$hydra-gt 7777];then
echo "Hydra"
Else
echo "Mdzz"
Fi
Test: Guess the number game
[Email protected] ~]# vim b.sh
#!/bin/bash
SUIJI=\$[RANDOM%100]
For i in {1..9} (i is a variable representing all integers from 1 to 9, representing a maximum of 9 guesses)
Do
If [\$shuo-eq \$suiji];then
echo "Hail Hydra"
Exit
elif [\$shuo-gt \$suiji];then
echo "Mdzz"
Else
echo "Mdzz"
Fi
Done

Multi-branch format: (Judgment not satisfied, re-judge to execute next condition, and so on)
If [Judge]&&[judgment];then
Command
elif[Judgment]&&[judgment];then
Command
Else
Command
Fi

[Email protected] ~]# vim c.sh
#!/bin/bash
If [\$dj-ge]&&[\$dj-le];then
echo "\$DJ level! Dalao "
elif [\$dj-ge]&&[\$dj-le];then
echo "\$DJ level! Emmm "
Else
echo "\$DJ level! Mdzz "
Fi

——————————————————————————————————

Shell script Base numeric operation judgment and if statement

Related Keywords:
Related Article

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.