[root@localhost code]# expr 1+2
1+2
[root@localhost code]# expr 1 + 2
3
[root@localhost code]# num1 = 10
-bash: num1: command not found
[root@localhost code]# num1=10
[root@localhost code]# num2=20
[root@localhost code]# expr $num1 + $num2
30
将运算值赋值给变量,需要先执行
[root@localhost code]# sum=`expr $num1 + $num2`
[root@localhost code]# echo $sum
30
乘法运算需要转义
[root@localhost code]# expr $num1 * $num2
expr: syntax error
[root@localhost code]# expr $num1 \* $num2
200
参考脚本
[root@localhost code]# free -m
total used free shared buff/cache available
Mem: 3770 130 3397 11 242 3374
Swap: 2047 0 2047
[root@localhost code]# free -m |grep '^Mem' # 查找以Mem开头的行
Mem: 3770 130 3397 11 242 3374
[root@localhost code]# free -m |grep '^Mem' |awk '{print $6}' # 透过awk,打印第6列
242
[root@localhost code]# buff_cache=`free -m |grep '^Mem' |awk '{print $6}'` # 透过反引号将执行结果赋值给变量
[root@localhost code]# echo $buff_cache
242
[root@localhost code]# cat mem_test.sh
#!/usr/bin/bash
mem_used=`free -m |grep '^Mem' |awk '{print $3}'`
mem_total=`free -m |grep '^Mem' |awk '{print $2}'`
mem_percent=$((mem_used * 100 / mem_total))
echo "当前内存使用百分比:$mem_percent"
[root@localhost code]#
[root@localhost code]# ./mem_test.sh
当前内存使用百分比:3
[root@localhost code]# cat ping.sh
#!/usr/bin/bash
web="www.baidu.com"
for i in {1..5}
do
ping -c1 $web &> /dev/null
if [ $? -eq 0 ]; then
echo "ping $web succeed"
fi
done
[root@localhost code]# chmod a+x ping.sh
[root@localhost code]# ./ping.sh
ping www.baidu.com succeed
ping www.baidu.com succeed
ping www.baidu.com succeed
ping www.baidu.com succeed
ping www.baidu.com succeed
[root@localhost code]# cat ping02.sh
#!/usr/bin/bash
ip=192.168.146.128
i=1
while [ $i -le 5 ]
do
ping -c1 $ip &> /dev/null
if [ $? -eq 0 ]; then
echo "$i th ping $ip succeed"
let i++
fi
done
[root@localhost code]#
[root@localhost code]# ./ping02.sh
1 th ping 192.168.146.128 succeed
2 th ping 192.168.146.128 succeed
3 th ping 192.168.146.128 succeed
4 th ping 192.168.146.128 succeed
5 th ping 192.168.146.128 succeed
通过python计算小数
通过bc计算小数
echo "5/2" |bc
以debug方式运行
[root@localhost code]# bash -vx ping02.sh
#!/usr/bin/bash
ip=192.168.146.128
+ ip=192.168.146.128
i=1
+ i=1
while [ $i -le 5 ]
do
ping -c1 $ip &> /dev/null
if [ $? -eq 0 ]; then
echo "$i th ping $ip succeed"
let i++
fi
done
+ '[' 1 -le 5 ']'
+ ping -c1 192.168.146.128
+ '[' 0 -eq 0 ']'
+ echo '1 th ping 192.168.146.128 succeed'
1 th ping 192.168.146.128 succeed
+ let i++
+ '[' 2 -le 5 ']'
+ ping -c1 192.168.146.128
+ '[' 0 -eq 0 ']'
+ echo '2 th ping 192.168.146.128 succeed'
2 th ping 192.168.146.128 succeed
+ let i++
+ '[' 3 -le 5 ']'
+ ping -c1 192.168.146.128
+ '[' 0 -eq 0 ']'
+ echo '3 th ping 192.168.146.128 succeed'
3 th ping 192.168.146.128 succeed
+ let i++
+ '[' 4 -le 5 ']'
+ ping -c1 192.168.146.128
+ '[' 0 -eq 0 ']'
+ echo '4 th ping 192.168.146.128 succeed'
4 th ping 192.168.146.128 succeed
+ let i++
+ '[' 5 -le 5 ']'
+ ping -c1 192.168.146.128
+ '[' 0 -eq 0 ']'
+ echo '5 th ping 192.168.146.128 succeed'
5 th ping 192.168.146.128 succeed
+ let i++
+ '[' 6 -le 5 ']'