Operation of variables in shell programming (Shell 06)

Source: Internet
Author: User
Tags arithmetic

Mainly includes the following 3

String manipulation
Mathematical operations
Floating point Arithmetic

One. String manipulation
Connection of Strings
Connection Word 2 strings do not need any connectors, write next to each other

Length acquisition
  Expr Length "Hello"
Expr Length "$str" variable name must be placed in double quotation marks, no person syntax error
find the position of a character in a string
Expr index "$STR" CHARS
The first one is starting from 1, finding no return 0, returning the position of the first character to match to

[Email protected] ~]#Echo$strhello Word[[email protected]~]#ExprIndex"$str"h1[[Email protected]~]#ExprIndex"$str"Hel (Match h only)1[[Email protected]~]#ExprIndex"$str"a0

String truncation
Expr substr "$str" POS LENGTH
POS start position (included), length

Expr " $str " 7 4 Word

String match
Expr "$str": REGEXP (spaces before and after a colon)
Expr mathch "$str" REGEXP
Must be fully matched.

echoexpr"$str"\ ([a-z]* [a-z]*\)]  ABcD PhP

Two. Mathematical operations
Logical operations
Numeric operations

Logical operations
&,|,<,>,=,!=,<=,>=
Numeric operations
+,-,*,/,%

Expr expression
Result=$[expression]

SH Echo $num 1, $num 2, $num 3 1,2,1shexpr $num 1<$num 22: No file or directory

There are spaces on both sides of the operator

[[Email protected]SH]#Expr$num 1\<$num 21<2[[Email protected]SH]#Expr$num 1 \<$num 21[[Email protected]SH]#Expr$num 1 =$num 31[[Email protected]SH]#Expr$num 1 =$num 20used in expr=to determine whether to wait in the []==

[Email protected] sh]# res=$[$num 1= $num 3]
-bash:1=1:attempted Assignment to Non-variable (Error token is "= 1")
[Email protected] sh]# res=$[$num 1== $num 3]
[Email protected] sh]# echo $res
1

Floating-point arithmetic
Built-in calculator BC
BC is able to identify:
Numbers (integer and floating point types)
Variable
Comments (in #开始的行 or/* */)
An expression
Programming statements (e.g. conditional judgment: If-then)
Function

Bc-q can ignore the version information and other prompt language
Scale can set precision

SH] # BC-qten/33scale =4/33.3333 num1=ten; num2=3num1/num23.3333quit


Using BC in Scripts
1.
Var= ' echo ' options;expression ' |BC '

 [[email protected] sh ]# res= ' echo   " SCALE=4;10/3  "  | BC "[[email protected]  sh ]# echo   $res  3.3333  2  .res  = ' Bc<<eoptionsstatementsexpressionse '  
SH] # res= ' BC <<E> a=> b=3> scale=4> c=a/b> C >shecho  $res3.3333

Operation of variables in shell programming (Shell 06)

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.