一、运算符号
+ | 加法 |
---|---|
- | 减法 |
* | 乘法 |
/ | 除法 |
% | 除法后的余数 |
** | 乘方 |
++ | 自加一 |
– | 自减一 |
< | 小于 |
<= | 小于等与 |
> | 大于 |
>= | 大于等与 |
== | 等于 |
+= | j+=i 就是j=j+i |
*= | j*=i就是j=j*i |
/= | j/=i就是j=j/i |
%= | j%=i就是j=j%i |
二、运算指令
(()) ##((a=1+2))
let ##let a=1+2
expr ##expr 1 + 2
bc
bc << EOF
1+2
EOF
$[] ##echo $[1+2]
printf '%0.2f' .3 ==== 0.30
printf '%0.3f' .3 ====0.300
printf '%0.4f' .3 ====0.3000
#!/bin/bash
for ((I=1;I<=10;I++))
do
((J+=I))
echo $J
done
脚本练习
要求
随机生成1到10之间的数,输入数字,输入数字偏大,显示数字大了,小了,提示小了,直到输入和随机数一致,结束
#!/bin/bash
LUCK_NUMBER=$[$RANDOM%10]
echo $LUCK_NUMBER
while true
do
read -p "Please input number:" NUM
if [ "$LUCK_NUMBER" -gt "$NUM" ]
then
echo $NUM is too small
elif [ "$LUCK_NUMBER" -lt "$NUM" ]
then
echo $NUM is too big
else
echo yes !!!
exit
fi
done
或者
#!/bin/bash
LUCK_NUMBER=$[$RANDOM%10]
echo $LUCK_NUMBER
while true
do
read -p "Please input number:" NUM
if (($LUCK_NUMBER > $NUM))
then
echo $NUM is too small
elif (($LUCK_NUMBER < $NUM))
then
echo $NUM is too big
else
echo yes !!!
exit
fi
done
时钟倒计时脚本
#!/bin/bash
MIN=1
SEC=10
SECALL=$[$MIN*60+$SEC]
for ((;SECALL>0;SECALL--))
do
SHOWMIN=$[$SECALL/60]
SHOWSEC=$[$SECALL%60]
if (($SHOWSEC < 10))
then
echo -ne "$SHOWMIN:0$SHOWSEC"
sleep 1
echo -ne "\r"
else
echo -ne "$SHOWMIN:$SHOWSEC"
sleep 1
echo -ne "\r"
fi
done