大数据兼云计算(王明龙)讲师-LINUX-DAY20-SHELL-流程控制FOR

**

FOR循环

**


For循环语句(in右边向左边赋值)

For 语法:
-----------------------------------------------------------------------------
for 变量 in  值1  值2 ...
do               赋值执行动作
   commod
done

两种方法
--------------------------------------------------------------------------
第一种
--------------------------------------------------------------------------
for  ((i=1;i<=10;i++)) 
do
     command
done

解析:第一个位置i=1是定义变量,第二个位置i<=10是进行判断,第三个位置i++是循环赋值

--------------------------------------------------------------------------
第二种
--------------------------------------------------------------------------
for i in {1..10}  或  for i in 1 2 3 ..10
do
     command
done


解析:
---------------------------------------------------------------------
1) for :循环为
2) in:赋值
3) do:执行
4) done:完成循环,结束语
5) true:永为真,永不停止循环执行
6) fasle:永为假,永不被执行


格式:
---------------------------------------------------------------------
无双引
--------------------------------------------------------------
for  变量名 in  变量值1  变量值2 .........类推
do
      可执行命令列表
done 


有双引
------------------------------------------------
for  变量名 in  “变量值1  变量值2do
      可执行命令列表
done 







举例:
--------------------------------------------------------------------------

举例1(加双引号)

[root@localhost root]# vi l.sh
-----------------------------------------------
#!/bin/bash
for wml in "wang ming long"
do
    echo $wml
done

[root@localhost root]# . l.sh 
wang ming long


举例2(不加双引号)

[root@localhost root]# vi l.sh
-----------------------------------------------
#!/bin/bash
for wml in wang ming long
do
echo $wml
done

[root@localhost root]# . l.sh
 wang
 ming
 long


举例
[root@localhost shell]# vim a.sh
------------------------------------------------------
#!/bin/bash
a="/etc/passwd"
b="/etc/shadow"
c="/etc/group"

for i in $a $b $c
do
   cat $i
done 
------------------------------------------------------
[root@localhost shell]# chmod +x a.sh
[root@localhost shell]# ./a.sh

[root@localhost shell]# vim b.sh
------------------------------------------------------
#!/bin/bash
a=`cat /etc/passwd | grep root`
b=`cat /etc/shadow | grep wml`
c=`cat /etc/group | grep root`

for i in $a $b $c
do
    echo $i
done
------------------------------------------------------
[root@localhost shell]# chmod +x b.sh
[root@localhost shell]# ./a.sh


来几个嵌套
[root@node1 ~]# vim gj.sh         //先复制文件
#!/bin/bash
a="/etc/passwd"
b="/etc/shadow"
c="/etc/group"
n=`ls -l /etc | wc -l`
for i in $a $b $c
do
  if [ $n > 0 ]
  cp $i /root
done

[root@node1 ~]# vim gj.sh          //在删除文件
#!/bin/bash
a="/root/passwd"
b="/root/shadow"
c="/root/group"
n=`ls -l /etc | wc -l`
for i in $a $b $c
do
  if [ $n > 0 ]
     then
        rm -f $i
  else
        echo "is 0"
  fi
done


统计http服务是否启动,并加上标题符号
[root@node1 script]# vim a.sh
#!/bin/bash
a=`ps -ef | grep "httpd" | awk '{print $8}'| sort -u`
for i in \({1..6}\)$a
do
  echo $i
  #continue
  #break
done

[root@node1 script]# ./a.sh  
(1)/usr/sbin/httpd
(2)/usr/sbin/httpd
(3)/usr/sbin/httpd
(4)/usr/sbin/httpd
(5)/usr/sbin/httpd
(6)/usr/sbin/httpd


自动安装卸载httpd,并自动启停
[root@node1 script]# cat a.sh
#!/bin/bash
fun1 (){
a=`ps -ef | grep "httpd" | grep -v "grep" | wc -l`
for i in $a
do
  if [ $i -gt 0 ]
  then
     systemctl stop httpd
     echo "httpd is stoped"
     p=`ps -ef | grep httpd| grep -v "grep" | wc -l`
     if [ $p -eq 0 ]
     then
        r=`rpm -qa | grep httpd`
        rpm -e --nodeps $r
     fi
  elif [ $i = 0 ]
  then
     yum -y install httpd
     systemctl start httpd;echo "httpd is started"
  fi
  #continue
  #break
done
}

猜你喜欢

转载自blog.csdn.net/wangminglong1989/article/details/81509461