Linux Operating System Basics (ix)

Source: Internet
Author: User
Tags arithmetic arithmetic operators

operator and process Control for shell scripts 1. operator

1.1 Arithmetic operators

+

-

*

/

%

[[email protected] ~]# echo $[3+1]
4

1.2 Relationship Operations

With (())

<

>

<=

>=

==

!=

&&

||

Test command related, [] can achieve the same effect
[Email protected] ~]# x=1
[Email protected] ~]# [$x-gt 1]
[[email protected] ~]# echo $?
0

1.3 Assignment operators

=

+=

*=

/=

%=

[Email protected] ~]# x=10
[Email protected] ~]# ((x%3))
[Email protected] ~]# echo $x
10
[Email protected] ~]#
[Email protected] ~]# ((x%=3))
[Email protected] ~]# echo $x
1

1.4 All the calculators in the shell
$[] (()) $ (()) expr BC Bc-l

Floating point arithmetic: Yum install bc-y

[Email protected] ~]# echo ' SCALE=2;1/3 ' |bc-l
.33

1.5 Test actions

When the command executes, it returns to a system variable of $?
A value of 0 indicates that the command executed successfully or failed.


Testing command test [] [[]] (())
Open man test to describe each parameter individually
1.6. test file status
-D Directory
-S file length > 0, non-empty
-F Regular File
-W Writable

-R Readable

-X Executable

-L Symbolic connection

-u file has suid bit settings

1.7. String test
= Two strings equal
! = two strings are not equal
-Z Empty string
-N Non-empty string

[[email protected] ~]# var1= ' abc '
[[email protected] ~]# var2= ' 123 '
[[Email protected] ~]# [$var 1 = = $var 2]
[[email protected] ~]# echo $?
1

1.8. Test value
-eq equals
-ne Not equal to
-GT Greater than
-lt less than
-ge greater than or equal to
-le less than or equal to

[Email protected] ~]# [10000-GT] #不要使用大于号小于号等于号等, to use the man test specified, see the next section 4 expand
[[email protected] ~]# echo $?
0

1.9. Expand the test symbol [[]] (())
Digital Test Symbols
# [< 2] # syntax error
-bash:2: No file or directory
#

# [[2 > 10]] # result error
# echo $?
1
# [[> 10]] # Right
# echo $?
0
# ((< 20))
# echo $?
0

Character test
# ["AA" = "AA"]
# echo $?
0
# [["AA" = "AA"]]
# echo $?
0
# (("AA" = "AA")) #结果错误
# echo $?
1
Hybrid testing
# [A = A-a < 20]
-BASH:20: No file or directory
[[Email protected] ~]# [[a = A-a < 20]]
-bash:syntax Error in conditional expression
-bash:syntax error near '-a '
[[Email protected] ~]# [[a = a && < 20]]
[[email protected] ~]# echo $?
0
[[Email protected] ~]# [[a = a | | < 20]]
[[email protected] ~]# echo $?
0
[[Email protected] ~]# ((a = a | | < 20))
[[email protected] ~]# echo $?
0
[[Email protected] ~]# ((a = a && < 20))
[[email protected] ~]# echo $?
0

Two. Process Control

Use of 1.if

If is usually

If [Judging condition];then #如果条件满足那么

echo ' Output content ' #输出内容

Else

echo ' Output content ' #否则输出

Fi #fi来关闭

You can also add elif to make multiple judgments.

2. Cycle control

while (condition)

Do action

Done

If the while is followed by: then this control goes into an infinite loop.

For loop

For i in [1..100]

Do

Done

Syntax structure for A for loop

In the Loop

Use continue to cycle through a layer of exit operations.

Break is a piece of the loop that exits the operation.

Linux Operating System Basics (ix)

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.