红联Linux门户
Linux帮助

linux下运算的几种方法

发布时间:2017-03-28 10:36:35来源:linux网站作者:王李
1.expr
1.1.最简单的用法
yan@yan:~$ expr 4 \* 5
20
yan@yan:~$ 
 
1.2.bash脚本对于expr
yan@yan:~$ cat expr1.sh 
#!/bin/bash
PATH=/home/yan/bin:/home/yan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
export PATH
var1=100
var2=200
var3=$(expr $var2 / $var1)
echo "var3=$var3"
yan@yan:~$
yan@yan:~$ chmod 755 expr1.sh 
yan@yan:~$ ./expr1.sh 
var3=2
yan@yan:~$
 
2.方括号
2.1.最简单的用法
yan@yan:~$ echo "$[3+2]"
5
yan@yan:~$
 
2.2.bash对于脚本[]
yan@yan:~/leran/shell$ cat test1.sh 
#!/bin/bash
PATH=/home/yan/bin:/home/yan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
export PATH
var1=100
var2=200
var3=$[$var2 / $var1]
echo "$var3"
yan@yan:~/leran/shell$ chmod 755 test1.sh 
yan@yan:~/leran/shell$ ./test1.sh 
2
yan@yan:~/leran/shell$
 
3.bc
3.1.最简单的用法
yan@yan:~/leran/shell$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
3+2-5
0
quit
yan@yan:~/leran/shell$ 
其中:bc进入软件,quit退出软件
 
3.2.bash对于bc
yan@yan:~/leran/shell$ cat bc.sh 
#!/bin/bash
PATH=/home/yan/bin:/home/yan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
export PATH
var1=200
var2=199
var3=$(echo "scale=5;$var1/$var2" | bc)
echo $var3
yan@yan:~/leran/shell$ chmod 755 bc.sh 
yan@yan:~/leran/shell$ ./bc.sh 
1.00502
yan@yan:~/leran/shell$
 
本文永久更新地址:http://www.linuxdiyf.com/linux/29537.html