当前位置:网站首页>6、 Symbols and commands for numerical calculation of variables
6、 Symbols and commands for numerical calculation of variables
2022-06-24 01:40:00 【jackxiao】
1. Common arithmetic operators
Symbol | explain |
|---|---|
+、- | plus 、 minus sign |
*、/、% | Multiplication 、 Touch method 、 Remainder |
** | Nether operation |
++、-- | Increase or decrease 、 Reduce |
!、&&、|| | Logic is not ( Take the opposite )、 Logic and (and)、 Logic or (or) |
<、<=、>、>= | Comparative symbols ( Less than 、 Greater than ) |
==、!=、= | Comparative symbols ( equal 、 It's not equal 、 amount to ) |
<<、>> | Shift left 、 Shift right |
~、|、&、^ | According to the not 、 Exclusive or 、 And 、 or |
=、+=、-=、*=、/+、%= | The assignment operation (a+=1 amount to a=a+1) |
2. Common arithmetic operation commands
Symbol | explain |
|---|---|
(()) | Common operators for integer operations , It's very efficient |
let | For integer operations , Be similar to "(())" |
expr | Can be used for integer operations , There are many other features |
bc | linux Calculator program under , Suitable for integer and decimal |
$[] | For integer operations |
awk | awk Can be used for integers , It can also be used for decimal operations |
declare | Define variable values and properties ,-i Parameters can be used to define shaping variables , Do calculations |
Two 、 Calculation practice
1. Double brackets
1) Routine usage
Symbol | explain |
|---|---|
((i=i++)) | Operation after assignment |
((i=++i)) | Evaluate first and then assign |
i=$((i+1)) | Assign values to variables after operation i |
((a>7&&b<5)) | Do a comparison operation , You can also make conditional judgments |
echo $((2+1)) | The result of the world output expression |
++、-- Memory method of operation : i=i++ It means right first i assignment , And then do the self addition operation , That is, the actual i Value than output i Large value i=++i Indicates that the self addition operation is performed first , In the face of i assignment , That is, the actual i And the output of i equally
2)i++ and ++i test
- i=6;echo $((i++));echo $i
6 7
- i=6;echo $((++i));echo $i
7 7
2. Other calculation commands
1) let Assignment expression
let i=i+2 Equate to ((i=i+2))
2) bc Calculation command
bc yes linux Under the computer , The matching pipe is often used for calculation , You can perform decimal operations
echo "1+9"|bc i=5;i=`echo $i+6|bc`
3) awk Do arithmetic
Decimals can be made 、 The operation of integers , useful echo "7.7 9.8"|awk '{print ($1+$2),($1*$2)}'
17.5 75.46
4) $[] Do calculations
echo $[4+2] $[4*2] $[4**2]
6 8 16
3、 ... and 、 Case study
1. Achieve output 1+2+3..+10=55 Calculation and output of
- Method 1:
echo `seq -s "+" 10`=`seq -s + 10|bc`
1+2+3+4+5+6+7+8+9+10=55
- Method 2:
echo `echo {1..10}|tr " " "+"`=`echo {1..10}|tr " " "+"|bc`1+2+3+4+5+6+7+8+9+10=55
- Method 3:expr
echo `seq -s + 10`=`seq -s " + " 10|xargs expr`
1+2+3+4+5+6+7+8+9+10=55
- Method 4:(())
echo `seq -s + 10`=$((`seq -s + 10`))
1+2+3+4+5+6+7+8+9+10=55
2.read Command to read parameters
1) read Command basis
- grammar :read [ Parameters ] [ Variable name ] -p Set the prompt message -t Set the input waiting time ( second )
read -t 10 -p "input tow num:" a b
input tow num:1 2 Set up 10 Second timeout , The prompt is **
2) Calculation script Demo
- cat /server/scripts/t.sh
#!/bin/bash read -p "num1:" a read -p "num2:" b echo "a-b=$(($a-$b))" echo "a+b=$(($a+$b))" echo "a*b=$(($a*$b))" echo "a/b=$(($a/$b))" echo "a**b=$(($a**$b))"
After allowing scripts , Prompt the function of entering two strings respectively
3) The script adds a judgment integer statement
- cat /server/scripts/t.sh
#!/bin/bash
#no.1
read -p "num1:" a
expr $a + 0 &>/dev/null
[ $? -ne 0 ] && {
echo "pls int"
exit 1
}
#no.2
read -p "num2:" b
expr $b + 0 &>/dev/null
[ $? -ne 0 ] && {
echo "pls int"
exit 2
}
#no.3
echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"For each input string , Verify that it is an integer , If it is not an integer, prompt and exit , After both are integers , Then calculate
边栏推荐
- Dart series: using packages in dart
- How does smart digital operation get through offline collection and online marketing?
- Behind the 1.6 trillion requests per day, the secret of DNSPod - state secret DOH
- S2b2c e-commerce platform in the pharmaceutical and medical industry enables enterprises to grasp differentiated competitive advantages and improve supply chain efficiency
- If the program exits abnormally, how to catch the fatal error through the go language?
- "Cross border" layout photovoltaic circle Green Island wind invested 4.6 million to layout distributed photovoltaic power generation
- Oracle sqlldr quick import and sqluldr2 quick export
- Is the domain name available for trademark registration? How long is the trademark registration cycle?
- How to write the domain name of trademark registration? What is the process of trademark and domain name registration?
- [log service CLS] experience of troubleshooting abnormal scenarios with tke event log
猜你喜欢

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)

【Flutter】如何使用Flutter包和插件

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
![[flutter] comment utiliser les paquets et plug - ins flutter](/img/a6/e494dcdb2d3830b6d6c24d0ee05af2.png)
[flutter] comment utiliser les paquets et plug - ins flutter
随机推荐
Tencent cloud Weibo was selected into the analysis report on the status quo of China's low code platform market in 2021 by Forrester, an international authoritative research institution
Video stream playback address redirection optimization after easycvr replaces the new kernel
What is the website construction process? What details need to be valued?
Golang gets the start timestamp and end timestamp of a past or future week or month
Use Navicat software to connect self built database (Linux system)
Note 3 of disruptor: basic operation of ring queue (without disruptor class)
[planting grass by technology] three big gifts prepared by Tencent cloud for you on the double 11, welcome to touch~
How to use the speech synthesis assistant? Does speech synthesis cost money?
How to restart the server through the fortress machine how to log in to the fortress machine
How to choose a website construction company self-study website or website construction company
Software cost evaluation: basic knowledge interpretation of cosmoc method
EasyPlayer. JS play m3u8 error net:: err_ EMPTY_ Response, how to solve it?
Glusterfs version 4.1 selection and deployment
CLB O & M & operation best practices - big insight into access logs
How to build high quality and high brand websites what should be paid attention to in the construction of enterprise websites
[JS reverse hundred examples] md5+aes encryption analysis of an easy payment password
Is the domain name available for trademark registration? How long is the trademark registration cycle?
How to implement NSQ delay streaming technology in easycvr?
Technology sharing | Clickhouse cluster's way of expanding replicas under sharding
What is the cost of domain name trademark registration? What is the use of domain names and trademarks?