for循环是最简单,也是最常用的循环语句。与其他的程序设计语言一样,for循环都是初学者在学习循环结构时的入门课程。for循环通常用于遍历整个对象或者数字列表。按照循环条件的不同,for循环语句可以分为带列表的for循环、不带列表的for循环以及类C风格的for循环。
带列表的for循环通常用于将一组语句执行已知的次数,其基本语法如下:
for variable in list
do
statement1
statement2
...
done
在上面的语法中,variable称为循环变量,list是一个列表,可以是一系列的数字或者字符串,元素之间使用空格隔开。do和done之间的所有的语句称为循环体,即循环结构中重复执行的语句。for循环体的执行次数与list中元素的个数有关。在带列表的for语句执行时,Shell会将in关键字后面的list列表的第1个元素的值赋给变量variable,然后执行循环体;当循环体中的语句执行完毕之后,Shell会将列表中的第2个元素的值赋给变量variable,然后再次执行循环体。当list列表中的所有的元素都被访问后,for循环结构终止,程序将继续执行done语句后面的其他的语句。
Shell允许用户指定for语句的步长。当用户需要另外指定步长时,其基本语法如下:
for varibale in {start..end..step}
do
statement1
statement2
...
done
在某些特殊情况下,for循环的条件列表可以完全省略,称为不带列表的for循环语句。如果没有为for循环提供条件列表,Shell将从命令行获取条件列表。不带列表的for循环语句的一般语法如下:
for variable
do
statement1
statement2
...
done
由于系统变量$@同样可以获取所有的参数,所以以上的语法等价于以下语法:
for variable in $@或$*
do
statement1
statement2
...
done
for ((expression1;expression2;expression3))
do
statement1;
statement2;
...
done
在上面的语法中,for循环语句的执行条件被2个圆括号包括起来。执行条件分为3个部分,由2个分号隔开,第1部分expression1通常是条件变量初始化的语句;第2部分expression2是决定是否执行for循环的条件。当expression2的值为0时,执行整个循环体;当expression2的值为非0时,退出for循环体。第3部分,即表达式expression3通常用来改变条件变量的值,例如递增或者递减等。
while循环是另外一种常见的循环结构。使用while循环结构,可以使得用户重复执行一系列的操作,直到某个条件的发生。这听起来好像跟until循环非常相似,但是与until语句相比,while语句有着较大的区别。
while循环语句的基本语法如下:
while expression
do
statement1
statement2
...
done
今日作业:
[redhat@localhost day4]$ vim task_99_2.sh
#!/bin/bash
for x in {1..9}
do
for y in {1..9}
do
if [ $y -le $x ]
then
echo -n -e $x*$y="$(($x*$y))\t"
fi
done
echo
done
[redhat@localhost day4]$ bash task_99_1.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
for循环(c语言风格)
[redhat@localhost day4]$ vim task_99_2.sh
#!/bin/bash
for ((i=1;i<10;i++))
do
for ((j=1;j<=i;j++))
do
echo -n -e $i*$j="$(($i*$j))\t"
done
echo
done
[redhat@localhost day4]$ bash task_99_2.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
while循环
[redhat@localhost day4]$ vim task_99_3.sh
#!/bin/bash
n=1
m=1
while (($n<10))
do
while (($m<$n+1))
do
echo -n -e "$n*$m=$(($n*$m)) "
let m++
done
let n++
m=1
echo
done
[redhat@localhost day4]$ bash task_99_3.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
2.使用for循环创建30个用户: test01~test30, 并设置密码为test01123456~test30123456
分析:
根据要求使用循环语句完成
创建用户使用useradd命令,根据命名规则,10以下的需要加0
初始账号设置方法(echo “123456” |passwd –stdin username)
[root@localhost day4]# vim add_users.sh
#!bin/bash
for user in test{01..30}
do
echo $user
if ! id -u $user &> /dev/null
then
useradd $user
echo '"$user"123456' | passwd --stdin $user
else
echo "$user is exits"
fi
done
[root@localhost day4]# bash add_users.sh
test01
Changing password for user test01.
passwd: all authentication tokens updated successfully.
test02
Changing password for user test02.
passwd: all authentication tokens updated successfully.
test03
Changing password for user test03.
passwd: all authentication tokens updated successfully.
test04
Changing password for user test04.
passwd: all authentication tokens updated successfully.
········
[root@localhost day4]# bash add_users.sh
test01
test01 is exits
test02
test02 is exits
test03
test03 is exits
test04
test04 is exits
test05
test05 is exits
·······
将创建的test01~test30用户删除
[root@localhost day4]# vim del_user.sh
for user in test{01..30}
do
echo $user
if id -u $user &> /dev/null
then
userdel -r $user
else
echo "$user is not exits..."
fi
done
[root@localhost day4]# bash del_user.sh
test01
test02
test03
test04
test05
test06
test07
3.使用循环去判断网段内的IP(1~254),本机除外,可以ping通的使用 ssh远程登录
本机的ip地址为172.25.10.129
[redhat@localhost day4]$ vim ssh.sh
#!bin/bash
for i in {1..254}
do
if [ $i -eq 129 ];then
continue
else
ping -c 2 -w 1 172.25.10.$i &> /dev/null
if [ $? -eq 0 ];then
ssh 172.25.10.$i
fi
fi
done
[redhat@localhost day4]$ bash ssh.sh
4.使用 @ 和 @和 @和*作为for循环后的列表,并体现出区别
[redhat@localhost day4]$ vim test4.sh
#!bin/bash
for i in "$@"
do
echo $i
done
echo "------------------"
for j in "$*"
do
echo $j
done
[redhat@localhost day4]$ bash test4.sh 1 2 3 4
1
2
3
4
------------------
1 2 3 4
5.使用循环去读取文件内容并输出: 3种方式(1.exec+while循环 2.管道符+while循环 3.重定向+while)
[redhat@localhost day4]$ cat file.txt
hello1
hello2
hello3
hello4
hello5
方法1:exec+while循环
[redhat@localhost day4]$ vim content1.sh
#!/bin/bash
exec < file.txt
while read file
do
echo $file
done
[redhat@localhost day4]$ bash content1.sh
hello1
hello2
hello3
hello4
hello5
方法2:管道符+while循环
[redhat@localhost day4]$ vim content2.sh
#!/bin/bash
cat file.txt | while read file
do
echo $file
done
[redhat@localhost day4]$ bash content2.sh
hello1
hello2
hello3
hello4
hello5
方法3:重定向+while循环
[redhat@localhost day4]$ vim content3.sh
#!/bin/bash
while read file
do
echo $file
done < file.txt
[redhat@localhost day4]$ bash content3.sh
hello1
hello2
hello3
hello4
hello5