"Learn Linux with older boys Koriyuki shell programming Practice"-the fourth chapter of variable numerical calculation

Source: Internet
Author: User
Tags arithmetic arithmetic operators

This article explains the numerical calculation of variables in shell programming.

1. Common Arithmetic operators:

650) this.width=650; "Width=" "height=" 216 "title=" 2.png "style=" WIDTH:600PX;HEIGHT:216PX; "alt=" Wkiom1j4rnrydwfjaabg6obovmq759.png "src=" https://s4.51cto.com/wyfs02/M01/91/C5/ Wkiom1j4rnrydwfjaabg6obovmq759.png "border=" 0 "vspace=" 0 "hspace=" 0 "/>

Tips:

Here for myself, + + 、--more difficult to understand, the previous see the script has these symbols, you can not understand, so here is an example:

[[email protected "~]# a=10                    ==> defines variable a
[[email protected] ~]# echo $ ((a++))    

==> If A is in front of the operator + + or--then the output of the entire expression will output the value of a, the variable a previously defined is 10, so the value here is ten.
10
[[email protected] ~]# echo $a ==> after performing the above expression, because there is a++, a will increment by 1, so the output value is one
11
[[email protected] ~]# a=11 ==> define variable a

[[email protected] ~]# echo $ ((a--))

==> if A is in front of the operator + + or--then the output of the entire expression will output the value of a, and the previous definition of a is 11, so the value here is 11.

11
[[email protected] ~]# echo $a ==> after performing the above expression, because there is a--, a will be self-minus 1, so the output value is ten
10
[Email protected] ~]# a=10
[[email protected] ~]# echo $ ((--a))

==> If A is behind the operator + + or--then the output of the entire expression is calculated as self-increment or decrement, since a is 10 and is to be self-decrement, so the value of the expression is 9.

9
[[email protected] ~]# echo $a ==> after performing the above expression, a is reduced by 1, so the value of the expression is 9.

9
[[email protected] ~]# echo $ ((++a))

==> If A is behind an operator + + or--then output an entire expression with a self-increment or decrement calculation, since a is 9 and is going to increment by 1, so the value of the expression is 10

10
[[email protected] ~]# echo $a ==> after performing the above expression, a is increased by 1, so the value of the expression is ten

10

Summarize:

When you perform the Echo $ ((a++) and Echo $ ((a--)) command to output the entire expression, the value of the output is the value of a, and after the expression is executed, a + + 、--operation is performed, and the

When the Echo $ ((++a)) and echo $ (--a) command output the entire expression, the A is first performed with the + + 、--, and then the value of the expression is output, which is the value after the a operation.

Memory formula:

Variable a precedes the operator, the value of the output expression is a, then a is self-increasing or self-decreasing;

Variable A after the operator, the output expression is self-increment or decrement, and the value of the expression is the value of a after self-increment or decrement.

Common Arithmetic operations Commands:

650) this.width=650; "Width=" height= "title=" 3.png "style=" width:600px;height:160px; "alt=" Wkiol1j4rq7d2ojxaabxt-afqyw384.png "src=" https://s4.51cto.com/wyfs02/M01/91/C4/ Wkiol1j4rq7d2ojxaabxt-afqyw384.png "border=" 0 "vspace=" 0 "hspace=" 0 "/>

(1) Double parenthesis "(())" Numeric Operation command

The underlying syntax for numeric operations of the Double parenthesis "(())"

The function of the double parenthesis "(())" is to carry on the numerical computation and the numerical comparison, the efficiency is very high, the usage is flexible, is the enterprise scene operation personnel frequently uses the operation operator.

The operation method is shown in the following table:

650) this.width=650; "Width=" "height=" 135 "title=" 4.png "style=" width:600px;height:135px; "alt=" Wkiol1j4seeditaraabhadkw-k8876.png "src=" https://s2.51cto.com/wyfs02/M01/91/C4/ Wkiol1j4seeditaraabhadkw-k8876.png "border=" 0 "vspace=" 0 "hspace=" 0 "/>

Tips:

(()) When comparing operations, if "Echo $ ((2>1)" Returns a result of 1 is true, if the return result is 0, then false.

The "(())" Expression does not need to add a $ sign at the command line and is used directly ((2+3)), but if output is required, add $

Symbols, such as: Echo $ ((2+3)).

There is no space between all the characters in (()), and one or more spaces do not affect the result.

(2) Usage of Let Operations command

Syntax format for let Operations Command: let assignment expression

The function of let assignment expression is equal to "((Assignment expression))"

Let assignment: For example: Let i=i+2 ==> equals ((i=i+2))

(3) Use of the expr command

Basic usage of the expr command:

Expr can be used for integer operations as well as for arithmetic processing of related string lengths, matches, and so on.

Apply 1:expr to calculate

Syntax: Expr Expression ==>expression can contain variables

For example:

[[email protected] ~]# Expr 1 + 1
2
[[email protected] ~]# Expr 1 \* 1
1

Note: When using expr, be aware of the following 2 points:

    • The operator and the number used for the calculation have at least one space around it, or they will get an error.

    • When using multiplication sign, you must escape with a backslash.

Application 2: Determine whether a variable value or string is an integer

This application takes advantage of the rule that a variable or character must be an integer when evaluated with expr, adding a variable or string to a known integer (not 0).

See if the returned value is 0, and if 0, the variable or string that makes the addition is an integer, otherwise it is not an integer.

In addition, the match function of expr can also be integer judged in the following format:

Expr Match $string substring

Matches the substring string in the $string string and returns the length of the matched substring string, or 0 if it is not found.

Apply 3:expr to determine if the file name extension meets the requirements:

To illustrate:

Determine if the. Pub is the extension:

[email protected] scripts]# cat expr.sh
#!/bin/sh
If expr "$" : ". *\.pub" &>/dev/null ==> Note: There should be a space before and after this colon
Then
echo "You are using $"
Else
echo "pls use *.pub file"
Fi

[Email protected] scripts]# sh expr.sh qq.pub
You is using qq.pub
[[Email protected] scripts]# SH expr.sh QQ
Pls use *.pub file

Application 4: Calculating the length of a string through expr

Syntax: expr length $ variable name or string

(4) Usage of BC commands

BC is a calculator under Linux, of course, besides being used as a calculator, it can also be used as a command-line calculation tool.

Here's how to use it:

[email protected] scripts]# BC
BC 1.06.95
Copyright 1991-1994, 1997, 1998, 2004, 2006 Free Software Foundation, Inc.
This is the free software with absolutely NO WARRANTY.
For details type ' warranty '.
2+3
5
3*3
9
[Email protected] scripts]# echo 2+3|BC
5
[Email protected] scripts]# echo "SCALE=2;9/2" | BC ==> Use scale=2 to retain 2 decimal places
4.50
[Email protected] scripts]# echo "scale=2;355/113" | Bc
3.14

(5) AWK implementation calculation

The effect of the awk operation is also good, suitable for decimals and integers, especially decimals, and the operation is very precise.

Examples are as follows:

[Email protected] ~]# echo "5 6" | awk ' {print ($1+$2)} '

==>$1 is the 1th digit, and the number is 2nd, separated by a space, note the single quote of awk ' {} '
11
[Email protected] ~]# echo "5.5 6.6" | awk ' {print ($1+$2)} '
12.1

(6) syntax of the declare (same typeset) command

The declare and typeset commands are built-in commands for Bash, which have the same syntax for declaring shell variables and setting the properties of the variables.

Common Command parameters:

    • -r: Set Variable to read-only

    • -I: Set Variable to Integer

    • -A: Sets a variable to an array of arrays

    • -F: If there are no parameters, then all functions defined by the previous script are listed, and if there are any parameters, the functions named by the parameters are listed.

    • -X: Setting variables can also be used outside the script

Simple to use as follows:

[Email protected] ~]# declare-i a=1 b=2
[Email protected] ~]# a=a+b
[Email protected] ~]# echo $A
3

(7) Example of operation of the $[] symbol

[[email protected] ~]# echo $[1+2]
3
[[email protected] ~]# echo $[1*2]
2

(8) Operation Practice of Input Read command based on shell variable

In addition to being able to assign values or script arguments directly, the shell script can be obtained from standard input using the Read command, which is read for bash

Built-in commands, just check the help by helping read.

Syntax format: read [parameter] [variable name]

Common parameters:

-P (Prompt): Set prompt information

-T (Timeout): Sets the input wait time, which is the default unit of seconds.

[Email protected] ~]# read-t 5-p "Pls input one num:" num

==> here num is the variable name, note that there is no need to add a $ symbol here, the same as a, B.
Pls Input One Num:2
[Email protected] ~]# read-t 5-p "Pls input" num: "a B
Pls Input 2 Num:1




Above for I read "with the old boy learn Linux Yun Koriyuki Shell programming Combat" This book when the note, if there are any copyright issues, please contact the message.


This article is from the "shayatou_1990" blog, make sure to keep this source http://shayatou1990.blog.51cto.com/12806916/1918020

"Learn Linux with older boys Koriyuki shell programming Practice"-the fourth chapter of variable numerical calculation

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.