linux shell数值运算

1 bc命令

1直接进入其命令行执行 bc -q 回车

2写个文件一起执行 bc -q bc-test.bc纯文本

3直接在shell中管道符交由bc执行数学运算 echo 2+3|bc

4可处理进制转换,设置精度

2 expr命令

可以是数字或者字符串

3let 命令

4$((expr))

5 ((a++))这种自加操作不用赋值,a从-变为了1,但是其他运算 $((b=b+5))这里是需要赋值的否则b不会变的

 

[lsx@tcbj3-slcdb01 tmp]$ echo 3+9|bc
12
[lsx@tcbj3-slcdb01 tmp]$ echo 3+9
3+9
[lsx@tcbj3-slcdb01 tmp]$ echo "scale=2;3%2"|bc
0
[lsx@tcbj3-slcdb01 tmp]$ echo "3%2"|bc
1
[lsx@tcbj3-slcdb01 tmp]$ echo "scale=2;3/2"|bc
1.50
[lsx@tcbj3-slcdb01 tmp]$ echo "ibase=16;A"|bc
10
[lsx@tcbj3-slcdb01 tmp]$ expr 3+9
3+9
[lsx@tcbj3-slcdb01 tmp]$ expr 3 + 9
12

[lsx@tcbj3-slcdb01 tmp]$ str=123454
[lsx@tcbj3-slcdb01 tmp]$ echo $str
123454
[lsx@tcbj3-slcdb01 tmp]$ echo $str+1
123454+1
[lsx@tcbj3-slcdb01 tmp]$ echo $(str+1)
-bash: str+1: command not found

[lsx@tcbj3-slcdb01 tmp]$ echo $str+1|bc
123455
[lsx@tcbj3-slcdb01 tmp]$ echo length $str
length 123454
[lsx@tcbj3-slcdb01 tmp]$ expr length $str
6
[lsx@tcbj3-slcdb01 tmp]$ expr substr $str 0 2

[lsx@tcbj3-slcdb01 tmp]$ echo $str
123454

[lsx@tcbj3-slcdb01 tmp]$ expr substr $str 1 3
123

[lsx@tcbj3-slcdb01 tmp]$ let a=2+9
[lsx@tcbj3-slcdb01 tmp]$ echo $a
11

[lsx@tcbj3-slcdb01 tmp]$ let a=2**3
[lsx@tcbj3-slcdb01 tmp]$ echo $a
8
[lsx@tcbj3-slcdb01 tmp]$ echo $((3+5))
8
[lsx@tcbj3-slcdb01 tmp]$ ((a=a+4))
[lsx@tcbj3-slcdb01 tmp]$ echo $a
4

[lsx@tcbj3-slcdb01 tmp]$ d=$((e=a+1))
[lsx@tcbj3-slcdb01 tmp]$ echo $e $d
5 5

[lsx@tcbj3-slcdb01 tmp]$ echo `date`
Wed Jun 12 16:08:57 CST 2019
[lsx@tcbj3-slcdb01 tmp]$ echo `date +%Y%m%d`
20190612

#注意空格
[dcadmin@tcbj3-dmdis01 ~]$ if [ "ok" == "ok" ];then echo "it\'s right";else echo "wrong"; fi
it\'s right

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值