In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Abstract:
1) Linux shell uses let, [], (()) three operators to operate shell variables for simple basic operations.
2) expr and bc are used to realize advanced operation in Linux shell.
1, the basic operation of Linux shell variable
The value is assigned directly to the variable as a regular variable and is saved as a string.
1.1 the let command can be used to perform basic operations directly:
When I use let, we don't use the $symbol to refer to variables.
No1=7
No2=8
Echo "- let command -"
Let no1++
Let no2--
Let no1+=3
Let no2-=5
Let result=no1+no2
Printf "let result=%d\ n" $result
The 1.2 "[]" operator is very similar to the let command:
The $symbol can be used in the "[]" operator to refer to variables, while supporting the absence of spaces between variable names and operators.
Echo "- [] operator -"
Printf "no1:%d no2:%d\ n" $no1 $no2
Result1=$ [no1 + no2]
Printf "result1 =% d\ n" $result1
Result2=$ [no1+no2 + 7]
Printf "result2 =% d\ n" $result2
Result3=$ [$no1+$no2+5]
Printf "result3 =% d\ n" $result3
No2=$ [no2 + 1]
Printf "no1 =% d no2 =% d\ n" $no1 $no2
1.3 the "()" operator is the same as the "[]" operator:
The use of the $symbol to reference variables for basic operations is also supported, and there is no space between the variable name and the operator.
Echo "(()) operator -"
Printf "no1:% d no2:% d\ n" $no1 $no2
Result1=$ ((no1 + no2))
Printf "result1 =% d\ n" $result1
Result2=$ ((no1+no2+3))
Printf "result2 =% d\ n" $result2
Result3=$ (($no1+$no2 + 5))
Printf "result3 =% d\ n" $result3
The "expr" command can also be used for the basic operation of variables:
The "expr" command also supports the $symbol to refer to variables for basic operations, but spaces must be used as delimiters between variables and operators.
After using the "expr" command to operate on the variable, the entire expression must be assigned to the variable using the pattern of "expression", that is, contained in the "`" character
And is equivalent to the "$(expression)" mode.
Echo "- expr command -"
Printf "no1:% d no2:% d\ n" $no1 $no2
Result1= `expr 3 + 4`
Printf "result1 =% d\ n" $result1
Result2= `expr $no1 + 4`
Printf "result2 =% d\ n" $result2
Result3= `expr $no1 + $no2`
Printf "result3 =% d\ n" $result3
Result4= `expr $no1+$ no2`
Printf "result4 =% d\ n" $result4
Result5=$ (expr $no1 + 3)
Printf "result5 =% d\ n" $result5
Result6=$ (expr $no1+4)
Printf "result6 =% d\ n" $result6
Result7=$ (expr $no1+$no2)
Printf "result7 =% d\ n" $result7
As shown by the experimental results in the above figure, in the expression of the "expr" command, a space must be used as a separator between the variable and the operator.
One thing I don't understand is why the expression on line 42 is wrong and why the error is reported on line 43.
"expr" also supports many operational expressions. Let's type an expr-help command in the terminal to have a look, o (∩ _ ∩) o .
Floating-point operations are not supported in the four ways of performing variable operations in shell described above!
2. Use the bc command in Linux shell to realize advanced mathematical operations:
The bc command uses standard input stdin as input
Bc is an advanced calculator that supports accurate floating-point operations
Bc has quite a number of input options and supports mathematical function calls
Execute bc-- help to see the input options supported by bc
The 2.1 bc command uses standard input stdin as input and supports floating-point operations:
Echo "- bc command -"
Echo "4 * 0.56" | bc
No=48
Result1= `echo "$no * 1.5" | bc`
Echo "result1 = $result1"
2.2 the bc command supports precision settings:
The operation precision can be specified for bc by adding parameters.
Additional parameters use semicolons ";" as delimiters
Result2= `echo "scale=9;$no / 3;" | bc`
Echo "result2 = $result2"
As shown in the experimental results, use the semicolon ";" to add the additional parameter scale and specify a precision of 9 decimal places.
2.3 use bc to convert numeric values between numbering decimal systems:
Specify the numeric base of the input variable by using ibase=value as an additional parameter
Specify the numeric base of the output variable through obase=value as an additional parameter
No=100
Echo "echo\" obase=2;ibase=10;$no\ "| bc = `echo" obase=2;ibase=10;$no "| bc`"
No=1000
Echo "echo\" scale=5;obase=10;ibase=2;$no/3\ "| bc = `echo" scale=5;obase=10;ibase=2;$no/3 "| bc`"
2.4 use bc to call mathematical formulas for advanced mathematical operations:
"sqrt (value)" performs the square operation of value
"value ^ index" performs the power operation of value
Echo "sqrt" = `echo "sqrt" | bc` "
Echo "sqrt (100) = $(echo" sqrt (100) "| bc)"
Echo "10 ^ 3 = `echo" 10 ^ 3 "| bc`"
Echo "10 ^ 3 = $(echo" 10 ^ 3 "| bc)"
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.