for循环语句
for循环语法结构
列表循环
列表for循环:用于将一组命令执行已知的次数
基本语法结构
#!/bin/bash
for i in a b c
do
echo $i
done
for i in {1..10}
do
echo $i
done
#!/bin/bash
for i in $(seq 10)
do
echo $i
done
#!/bin/bash
for i in $(seq 1 2 10) # 设置步长
do
echo $i
done
不带列表循环
不带列表的for循环执行时由用户指定参数和参数的个数
基本语法结构
for i
do
command
command
…
done
实例
#!/bin/bash
if [ $# -eq 0 ];then
echo "Usage: $0 加上参数"
fi
for i
do
echo $i
done
[root@maomao 3.27]# bash for.sh 1 2 3
1
2
3
#!/bin/bash
zhu=(2 4 6 8 10)
echo "${zhu[*]}"
fun() {
local niu=()
for i
do
niu[j++]=$[ $i * 5 ]
done
echo "${niu[@]}"
}
result=`fun ${zhu[@]}`
echo "新数组的所有值:${result[@]}"
类C风格的for循环
基本语法结构
for(( expr1;expr2;expr3 ))
do
command
command
…
done
for (( i=1;i<=5;i++))
do
echo $i
done
expr1:定义变量并赋初值
expr2:决定是否进行循环(条件)
expr3:决定循环变量如何改变,决定循环什么时候退出
循环控制语句
- continue:继续;表示循环体内下面的代码不执行,重新开始下一次循环
- break:打断;马上停止执行本次循环,执行循环体后面的代码
- exit:表示直接跳出程序
应用案例
九九乘法表
#!/bin/bash
# 方法一
# by stanZ
#!/bin/bash
for ((i=1;i<=9;i++))
do
for((n=1;n<=$i;n++))
do
echo -n "$i*$n=`expr $i \* $n` "
done
echo
done
#!/bin/bash
# 方法二
# by stanZ
#九九乘法表
for i in `seq 1 9`
do
for j in $(seq 1 $i)
do
echo -n "$i*$j=$[ $i * $j ]"
done
echo
#let i++
done
直角三角形
#!/bin/bash
# by stanZ
read -p "请输入三角形的高:" h
for((i=1;i<=$h;i++))
do
for j in `seq $i`
do
echo -n "*"
done
echo
done
等腰三角形
#!/bin/bash
# by stanZ
read -p "请输入三角形的高:" h
for((i=1;i<=$h;i++))
do
a=$[ $h - $i ]
for j in `seq $a`
do
echo -n " "
done
b=$((2*$i - 1 ))
for k in `seq $b`
do
echo -n "*"
done
echo
done
倒等腰三角形
#!/bin/bash
# by stanZ
read -p "请输入三角形的高:" h
for((i=0;i<=$h;i++))
do
for((j=1;j<=$i;j++))
do
echo -n " "
done
a=$(( 2*($h-$i) -1 ))
for((j=$a;j>=1;j--))
do
echo -n "*"
done
echo
done
菱形
#!/bin/bash
# by stanZ
read -p "请输入菱形半径:" h
for((i=0;i<=$h;i++))
do
a=$[ $h -$i ]
for((j=1;j<=$a;j++))
do
echo -n " "
done
b=$((2*$i - 1))
for((k=1;k<=$b;k++))
do
echo -n "*"
done
echo
done
for((u=1;u<=$h;u++))
do
for((n=1;n<=$u;n++))
do
echo -n " "
done
l=$(( 2*($h-$u)-1 ))
for((m=1;m<=$l;m++))
do
echo -n "*"
done
echo
done
空心三角形
#!/bin/bash
# by stanZ
read -p "请输入三角形的高:" h
for((i=1;i<=h;i++))
do
a=$[ $h - $i ]
for((j=1;j<=$a;j++))
do
echo -n " "
done
b=$[ 2*$i -1 ]
for((k=1;k<=$b;k++))
do
if [ $k -eq 1 ];then
echo -n "*"
elif [ $i -eq $h ];then
echo -n "*"
elif [ $k -eq $((2*$i-1)) ];then
echo -n "*"
else
echo -n " "
fi
done
echo
done
空心菱形
#!/bin/bash
# by stanZ
read -p "请输入菱形的半径:" h
for((i=1;i<=$h;i++))
do
a=$[ $h - $i ]
for((j=1;j<=$a;j++))
do
echo -n " "
done
b=$[ 2*$i - 1]
for((k=1;k<=$b;k++))
do
if [ $k -eq 1 -o $k -eq $b ];then
echo -n "*"
else
echo -n " "
fi
done
echo
done
for((u=1;u<=$h;u++))
do
for((n=1;n<=$u;n++))
do
echo -n " "
done
c=$((2*($h-$u)-1))
for((m=1;m<=$c;m++))
do
if [ $m -eq 1 -o $m -eq $c ];then
echo -n "*"
else
echo -n " "
fi
done
echo
done
脚本计算1-100奇数和
#!/bin/bash
# 方法1:
sum=0
for i in {1..100..2}
do
sum=$[$i+$sum]
done
echo "1-100的奇数和为:$sum"
# 方法2:
#!/bin/bash
sum=0
for ((i=1;i<=100;i+=2))
do
let sum=$i+$sum
done
echo "1-100的奇数和为:$sum"
# 方法3:
sum=0
for ((i=1;i<=100;i++))
do
if [ $[$i%2] -ne 0 ];then
let sum=$sum+$i
fi
或者
test $[$i%2] -ne 0 && let sum=$sum+$i
done
echo "1-100的奇数和为:$sum"
# 方法4:
sum=0
for ((i=1;i<=100;i++))
do
if [ $[$i%2] -eq 0 ];then
continue
else
let sum=$sum+$i
fi
done
echo "1-100的奇数和为:$sum"
#!/bin/bash
sum=0
for ((i=1;i<=100;i++))
do
test $[$i%2] -eq 0 && continue || let sum=sum+$i
done
echo "1-100的奇数和是:$sum"
判断所输整数是否为质数
#!/bin/bash
#定义变量来保存用户所输入数字
read -p "请输入一个正整数字:" number
#先排除用户输入的数字1和2
[ $number -eq 1 ] && echo "$number不是质数" && exit
[ $number -eq 2 ] && echo "$number是质数" && exit
#循环判断用户所输入的数字是否质数
for i in `seq 2 $[$number-1]`
do
[ $[$number%$i] -eq 0 ] && echo "$number不是质数" && exit
done
echo "$number是质数"
优化思路:没有必要全部产生2~$[$number-1]序列,只需要产生一半即可。
更好解决办法:类C风格完美避开了生成序列的坑
for (( i=2;i<=$[$number-1];i++))
do
[ $[$number%$i] -eq 0 ] && echo "$number不是质数" && exit
done
echo "$number是质数"
# 方法二
#!/bin/bash
read -p "输入一个数字:" num
if [ $num -eq 1 ];then
echo "$num不是质数"
exit 1
elif [ $num -eq 2 ];then
echo "$num是质数"
exit 2
fi
for i in `seq 2 $[$num-1]`
do
if [ $[$num%$i] -eq 0 ];then
echo "$num不是质数"
exit 3
else
echo "$num是质数"
exit 4
fi
done
批量创建用户
#!/bin/bash
read -p "请输入创建用户的数量:" num
read -p "请输入创建用户的前缀:" frefix
read -p "请输入创建用户的密码:" password
for((i=1;i<=$num;i++))
do
user=$frefix$i
useradd $user
echo "$password" |passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is created"
fi
done
测试网段存活主机
#!/bin/bash
# by stanZ
#for i in `seq 254`
#for i in {1..254}
read -p "请输入想检测的网段(默认192.168.188.0):" net
for((i=1;i<=254;i++))
do
{
if [ -z $net ];then
ip=192.168.188.$i
ping -c 3 -W 1 -i 0.2 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is up."
else
echo "$ip is donw."
fi
else
ip=$net.$i
ping -c 3 -W 1 -i 0.2 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is up."
else
echo "$ip is down."
fi
fi
}& # 放入后台并发执行
done
wait
echo "all finish.."