linux——shell 中的运算
生活随笔
收集整理的這篇文章主要介紹了
linux——shell 中的运算
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
運(yùn)算方式及運(yùn)算符號(hào)
| +,- | 加法,減法 |
| *,/,% | 乘法,除法,取余 |
| ** | 冪運(yùn)算 |
| ++,- - | 自增加,自減少 |
| <,<=,>,>= | 比較符號(hào) |
| =,+=,-=,*=,/=,%= | 賦值運(yùn)算,例如a+=1,相當(dāng)于a=a+1 |
SHELL 中常用的運(yùn)算命令
| (( )) | 用于整數(shù)運(yùn)算 |
| let | 用于整數(shù)運(yùn)算,與(())類似 |
| expr | 用于整數(shù)運(yùn)算,功能相對(duì)較多 |
| bc | linux 下的計(jì)算器,適合整數(shù)及小數(shù)運(yùn)算 |
| $[ ] | 用于整數(shù)運(yùn)算 |
示例用法
[root@desktop27 mnt]# (( a=1+1 )) [root@desktop27 mnt]# echo $a 2 [root@desktop27 mnt]# let a=1+2 [root@desktop27 mnt]# echo $a 3 [root@desktop27 mnt]# a=1+1 [root@desktop27 mnt]# echo $a 1+1 [root@desktop27 mnt]# echo `expr 1 + 1` 2 [root@desktop27 mnt]# 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'. 2+3 5 quit [root@desktop27 mnt]# bc << EOF > 2+3 > EOF 5 [root@desktop27 mnt]# echo $[1+1] 2 [root@desktop27 mnt]#10 秒倒計(jì)時(shí)腳本
[root@desktop27 mnt]# vim time.sh [root@desktop27 mnt]# cat time.sh #!/bin/bash for ((Time;Time>0;Time--)) doecho -n "after $Time's is end "echo -ne "\r \r"sleep 1 done [root@desktop27 mnt]# sh time.sh after 8's is end倒計(jì)時(shí)腳本一(可自定義倒計(jì)時(shí)時(shí)間)
##不建議使用 [root@desktop27 mnt]# vim time1.sh [root@desktop27 mnt]# cat time1.sh #!/bin/bash read -p "Please input minutes: " MIN read -p "Please input seconds: " SEC ((NUM=MIN*60+SEC)) for (($NUM;NUM>0;NUM--))dolet FF=NUM/60let MM=NUM%60echo -ne "\r$FF:$MM \r"sleep 1done [root@desktop27 mnt]#倒計(jì)時(shí)腳本二(可自定義倒計(jì)時(shí)時(shí)間)
[root@desktop27 mnt]# vim time.sh [root@desktop27 mnt]# cat time.sh #!/bin/bash PRING_MESSAGE() {echo -n "$MIN:$SEC "echo -ne "\r \r"sleep 1 } read -p "Please input MIN: " MIN read -p "Please input SEC: " SEC for ((;SEC>=0;SEC--)) do[ "$SEC" = "0" -a "$MIN" = "0" ]&& exit 0[ "$SEC" = "0" -a "$MIN" -gt "0" ]&&{PRING_MESSAGE((MIN--))SEC=59}PRING_MESSAGE done [root@desktop27 mnt]# sh time.sh Please input MIN: 1 Please input SEC: 10 1:7用腳本制作一個(gè)計(jì)算器,要求如下:
執(zhí)行 Calculator.sh 后顯示
Please input first number:
Please input the operation:
Please input second number:
執(zhí)行后顯示操作后的數(shù)值
總結(jié)
以上是生活随笔為你收集整理的linux——shell 中的运算的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux——shell 中常用的控制语
- 下一篇: linux——脚本的练习示例二