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)
[[email protected] ~]# Expr 45 \* 1

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

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
[[email protected] ~]# let a+=6
[Email protected] ~]# echo $a

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

Combination of commands:

Syntax format
[]: 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 (delete script)
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] ~]#/
You're really going to remove it, y/ny.

[[email protected] ~]# vim (monitor login user volume)
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)
[[email protected] ~]# crontab-e (added to scheduled task monitoring)
* * * * * */root/

[[email protected] ~]# vim (guessing word game)
xx=$ ((random%10)) (xx variable, the random number generated by the Linux system,%10 represents 10 of the number)
Read-p "hydra[0-10]:" Cai
[$cai-eq $xx]&& echo OK &&exit| | Echo Kill (when the number entered equals (eq) shows OK and exits, not kill)

[Email protected] ~]# vim
[!-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
.. ..
[Email protected] ~]# vim
if [$USER = root];then
Yum Clean All
Yum Repolist

Dual-branch format: (Judgment not satisfied, re-judge to perform the next condition)
If [Judge];then
elif [Judging];then
[Email protected] ~]# vim
Read-p code? : Hydra
If [$hydra-gt 9999];then
echo "Hail Hydra"
elif [$hydra-gt 8888];then
echo "Hail"
elif [$hydra-gt 7777];then
echo "Hydra"
echo "Mdzz"
Test: Guess the number game
[Email protected] ~]# vim
For i in {1..9} (i is a variable representing all integers from 1 to 9, representing a maximum of 9 guesses)
Read-p "hydra[0-99]" Shuo
If [$shuo-eq $suiji];then
echo "Hail Hydra"
elif [$shuo-gt $suiji];then
echo "Mdzz"
echo "Mdzz"

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

[Email protected] ~]# vim
Read-p "Level (1-100)": "DJ"
If [$dj-ge]&&[$dj-le];then
echo "$DJ level! Dalao "
elif [$dj-ge]&&[$dj-le];then
echo "$DJ level! Emmm "
echo "$DJ level! Mdzz "


Shell script Base numeric operation judgment and if statement

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: 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.