循环
循环 while
while list-1; do list-2; done
每2秒查看一下某个命令的执行结果
while true; do
w
sleep 2
done
while true; do uptime; sleep 2; done
######################## while 循环按行读取文件总结 ########################
exec < file.txt
sum=0
while read line; do
cmd
done
cat file.txt | while read line; do
cmd
done
while read line; do
cmd
done < file
$ cat file.txt
hello1
hello2
hello3
hello4
$ cat file.txt | while read line; do
echo "$line 123456"
done
循环 for
for name [ [ in [ word ... ] ] ; ] do list ; done
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
select name [ in word ] ; do list ; done
for ((i=1; i<=3; i++)); do
echo $i
done
for ((i=1; i<=3; i++)); do echo $i; done
for i in 5 4 3 2 1; do
echo $i
done
for i in 5 4 3 2 1; do echo $i; done
for i in {1..5}; do echo $i; done
for i in {5..1}; do echo $i; done
for i in `seq 5`; do echo $i; done
for i in `ls /tmp`; do echo $i; done
for i in `cat /tmp/hello.txt`; do echo $i; done
简单实践: 批量给文件名增加 .conf 后缀
$ touch conf{1..8}
$ ls
conf1 conf2 conf3 conf4 conf5 conf6 conf7 conf8
$ for i in `ls`; do mv $i ${i}.conf; done
$ ls
conf1.conf conf2.conf conf3.conf conf4.conf conf5.conf conf6.conf conf7.conf conf8.conf
循环控制 break continue ext return
break n
continue n
exit n
return n
for i in 5 4 3 2 1; do
echo $i
if [ $i -eq 3 ]; then
break
fi
done
for i in 5 4 3 2 1; do
# echo $i
if [ $i -eq 3 ]; then
continue
fi
echo $i
done
for i in 5 4 3 2 1; do
echo $i
if [ $i -eq 3 ]; then
exit
fi
done
输出99乘法表
输出99乘法表
[root@as4k scripts]# cat mul99.sh
#!/bin/bash
for ((x = 1; x <= 9; x = x + 1)); do
for ((y = 1; y <= x; y = y + 1)); do
printf "%dx%d=%d " $x $y $((x * y))
done
printf "\n"
done
当前在线的ip有哪些
写一个脚本实现判断192.168.1.0/24 网络里,当前在线的ip有哪些。
解答:
shell> cat tping.sh
#!/bin/bash
for i in 172.16.1.{1..254}; do
{
ping -c 2 $i &> /dev/null
if [ $? -eq 0 ]; then
echo $i is ok.
fi
} &
done
wait
简单计算器
简单计算器-交互式
整数计算
[root@as4k scripts]# cat cal-read.sh
#!/bin/bash
read -p 'Please input number1: ' n1
read -p 'Please input number2: ' n2
echo "for integer"
echo "$n1 + $n2 = $(( n1 + n2 ))"
echo "$n1 - $n2 = $(( n1 - n2 ))"
echo "$n1 * $n2 = $(( n1 * n2 ))"
echo "$n1 / $n2 = $(( n1 / n2 ))"
整数和小数计算
[root@as4k scripts]# cat cal-read.sh
#!/bin/bash
read -p 'Please input number1: ' n1
read -p 'Please input number2: ' n2
echo "for integer and float"
add=`echo "$n1 $n2" | awk '{print ($1 + $2)}'`
subtract=`echo "$n1 $n2" | awk '{print ($1 - $2)}'`
multiply=`echo "$n1 $n2" | awk '{print ($1 * $2)}'`
divide=`echo "$n1 $n2" | awk '{print ($1 / $2)}'`
echo "$n1 + $n2 = $add"
echo "$n1 - $n2 = $subtract"
echo "$n1 * $n2 = $multiply"
echo "$n1 / $n2 = $divide"
# 效果
[root@as4k scripts]# sh cal-read.sh
Please input number1: 2.3
Please input number2: 2.1
for integer and float
2.3 + 2.1 = 4.4
2.3 - 2.1 = 0.2
2.3 * 2.1 = 4.83
2.3 / 2.1 = 1.09524
简单计算器-传参式
[root@as4k scripts]# cat cal-read.sh
#!/bin/bash
echo "for integer and float"
add=`echo "$1 $2" | awk '{print ($1 + $2)}'`
subtract=`echo "$1 $2" | awk '{print ($1 - $2)}'`
multiply=`echo "$1 $2" | awk '{print ($1 * $2)}'`
divide=`echo "$1 $2" | awk '{print ($1 / $2)}'`
echo "$1 + $2 = $add"
echo "$1 - $2 = $subtract"
echo "$1 * $2 = $multiply"
echo "$1 / $2 = $divide"
# 效果
[root@as4k scripts]# sh cal-read.sh 2.3 22
for integer and float
2.3 + 22 = 24.3
2.3 - 22 = -19.7
2.3 * 22 = 50.6
2.3 / 22 = 0.104545