shell脚本编写-3

1、for循环

for 变量名 in  列表;do

循环体

done

执行机制:依次将列表中元素赋值给“变量名”;每次赋值后即执一次循环体;直到列表中元素耗尽循环结束

列表生成方式:

(1) 直接给出列表

(2) 整数列表:

(a){start..end}

(b) $(seq [start [step]] end)

 (3) 返回列表的命令

$(COMMAND)或者`COMMAND`

 (4) 使用glob,如:/root/bin/*.sh

 (5) 变量引用;

$@, $*

2、while循环

while CONDITION; do

循环体

done

   CONDITION(循环控制条件);进入循环之前,先做一次判断;每一次循环之后会再次做判断;条件为“true”,则执行一次循环;直到条件测试状态为“false”终止循环,因此CONDTION 一般应该有循环控制变量,而此变量的值会在循环体中不断地被修正

   进入条件:CONDITION为true

   退出条件:CONDITION为false

1、 until循环

until CONDITION; do

循环体

   done

   进入条件:CONDITION为false

   退出条件:CONDITION为true

2、 循环控制语句continue

用于循环体中

continue [N]:最内层为第1层,提前结束第N层的本轮循环,而直接进入下一轮判断

示例1:添加用户时,若某用户如user2存在,则结束当次if-fi,进入下一个用户user3的if-fi循环

#!/bin/bash

for username in user{1..10};do

      if grep "^$username\>" /etc/passwd;then

             continue

      else

             useradd $username

echo $username|passwd –stdin $username

      fi

done

示例2:#!/bin/bash

i=0

until false;do

echo $i;let i++;sleep 1;[ $i -eq 10 ] && continue;echo xx

done

输出的10后面没有xx,紧接着就输出11xx、12xx等等

由于sleep命令,例如0单独一次输出,而后是xx1一块输出,接着是xx2一块输出等等

3、 循环控制语句break

用于循环体中

   break [N]:提前结束第N 层整层循环,最内层为第1层

示例1:当输出9时,break所在的整层until循环结束,但是until外的echo xxxhhh命令仍然会执行,所有最后输出1-9和xxxhhh

#!/bin/bash

i=1

until false ;do

 [ $i -eq 10 ] && break

echo $i

let i++

done

           echo xxxhhh

4、 创建无限循环

while true; do

     循环体

done

until false; do

     循环体

   done 



练习题:

1、 判断/var/目录下所有文件的类型

第一种方法:

#!/bin/bash

for file in `ls -A /var`;do

        if [ -h /var/$file ];then

                echo "the file is link"

        elif [ -d /var/$file ];then

                echo "the file is directory"

        elif [ -f /var/$file ];then

                echo "the file is commen"

        else

                echo "the file is other"

        fi

done

第二种方法:

脚本filetype.sh

#!/bin/bash

read -p "please input the file path:" a

[ ! -e $a ] && echo "the file not exist" && exit

if [ -h $a ];then

echo "this file is link"

elif [ -d $a ];then

echo "this file is directory"

elif [ -f $a ];then

echo "this file is commen"

else

echo "this file is other"

fi

脚本filevartype1.sh

#!/bin/bash

for file in `ls -A /var`;do

        echo /var/$file |./filetype.sh

done

注意其中调用脚本时应写处调用脚本的绝对路径或者相对路径

2、 添加10个用户user1-user10 ,密码同用户名

#!/bin/bash

for username in user{1..10};do

  if grep "^$username\>" /etc/passwd;then

        continue

  else

        useradd $username;echo $username|passwd –stdin $username

  fi

done

3、/etc/rc.d/rc3.d 目录下分别有多个以K 开头和以S 开头的文件;分别读取每个文件,以K开头的文件输出为文件加stop,以S 开头的文件输出为文件名加start

“K34filename stop”

“S66filename start”

#!/bin/bash

for file in `ls -A /etc/rc.d/rc3.d`;do

   if echo $file |grep "^K" >>/dev/dull;then

      echo "$file stop"

   elif echo $file|grep "^S" >>/dev/dull;then

      echo "$file start"

   else

      echo "$file other"

   fi

done

4、 写 一个脚本,提示输入正整数n 的值,计算1+2+3+…n的总和

#!/bin/bash

sum=0

read -p "please input the positive integer:" a

for i in `seq 1 $a`;do

  sum=$[sum+i]

done

  echo "the total is $sum"

5、 写一个脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态

#!/bin/bash

read -p "please input the ip:" a

b=`echo $a|grep -o "^.*\."`

sumtrue=0

sumfalse=0

for ip in  {230..255};do

  ping -c1 -W1 $b$ip &>/dev/dull && let sumtrue++ || let sumfalse++

done

  echo "the ip is on:$sumtrue"

  echo "the ip is off:$sumfalse"

6、打印九九乘法表

#1/bin/bash

b=0

for i in {1..9};do

  let b++

  for j in `seq 1 $b`;do

     echo -en "$j*$i=$[j*i] \t" 内层循环不换行,但是在每次循环输出后面插入tab

  done

 echo 外层循环一次换一次行

done

1 、求100以内所有正整数之和

#!/bin/bash

i=1

sum=0

while [ $i -le 100 ];do

   sum=$[sum+i]

   let i++

done

   echo "the total is $sum"

2 、通过ping 命令探测10.1.252.230-254 范围内的所有主机的在线状态,统计在线主机和离线主机各多少

#!/bin/bash

b=`echo "10.1.252.0"|grep -o "^.*\."`

sumtrue=0

sumfalse=0

id=230

while [ $id -le 254 ];do

  ping -c1 -W1 $b$ip &>/dev/dull && let sumtrue++ || let sumfalse++

  let id++

done

  echo "the ip is on:$sumtrue"

  echo "the ip is off:$sumfalse"

3 、打印九九乘法表

#!/bin/bash

b=1

i=1

while [ $i -le 9 ];do

  j=1 注意j的初始值应该赋予此处,每次开始执行内循环时,j的值都从1开始

  while [ $j -le $b ];do

      echo -en "$j*$i=$[j*i] \t"

      let j++

  done

  let b++

  let i++

  echo

done

4 、利用变量RANDOM生成10 个随机数字,输出这个10 数字,并显示其中的最大者和最小者

#!/bin/bash

i=1

maxa=$RANDOM

mina=$maxa

echo $maxa

while [ $i -le 9 ];do

    mid=`echo $RANDOM`

    echo $mid

    if [ $mid -ge $maxa ];then

       maxa=$mid

    elif [ $mid -le $mina ];then

       mina=$mid

    fi

    let i++

done

    echo "the max is $maxa"

    echo "the min is $mina"

5、打印国际象棋棋盘

#!/bin/bash

i=1

while [ $i -le 8 ];do

   j=1

   while [ $j -le 8 ];do

     a=$[i%2]

     b=$[j%2]

     if [ $a -gt $b -o $a -lt $b ];then

     echo -en "\033[40m  \033[0m"

     let j++

     elif [ $a -eq $b ];then

     echo -en "\033[47m  \033[0m"

     let j++

     fi

   done

   let i++

   echo

done

1、 求100 以内所有正整数之和

#!/bin/bash

i=1

sum=0

until [ $i -gt 100 ];do

  sum=$[sum+i]

  let i++

done

  echo "the total is $sum"

2、 通过ping 命令探测10.1.252.230-254 范围内的所有主机的在线状态,统计在线主机和离线主机各多少

#!/bin/bash

b=`echo "10.1.252.0"|grep -o "^.*\."`

sumtrue=0

sumfalse=0

id=199

until [ $id -gt 254 ];do

  ping -c1 -W1 $b$ip &>/dev/dull && let sumtrue++ || let sumfalse++

  let id++

done

  echo "the ip is on:$sumtrue"

  echo "the ip is off:$sumfalse"

3、 打印九九乘法表

#!/bin/bash

b=1

i=1

until [ $i -gt 9 ];do

  j=1

  until [ $j -gt $b ];do

      echo -en "$j*$i=$[j*i] \t"

      let j++

  done

  let b++

  let i++

  echo

done

4、 利用变量RANDOM 生成10 个随机数字,输出这个10 数字,并显示其中的最大者和最小者

#!/bin/bash

maxa=$RANDOM

mina=$maxa

echo $maxai

until [ $i -ge 10  ];do

    mid=`echo $RANDOM`

    echo $mid

    if [ $mid -ge $maxa ];then

       maxa=$mid

    elif [ $mid -le $mina ];then

       mina=$mid

    fi

    let i++

done

    echo "the max is $maxa"

    echo "the min is $mina"

5、打印国际象棋棋盘

#!/bin/bash

i=1

until [ $i -gt 8 ];do

   j=1

   until [ $j -gt 8 ];do

     a=$[i%2]

     b=$[j%2]

     if [ $a -gt $b -o $a -lt $b ];then

     echo -en "\033[40m  \033[0m"

     let j++

     elif [ $a -eq $b ];then

     echo -en "\033[47m  \033[0m"

     let j++

     fi

   done

   let i++

   echo

done


 

 

原创文章,作者:18612763863,如若转载,请注明出处:http://www.178linux.com/37063

赞 (0)
1861276386318612763863
上一篇 2016-08-18 10:10
下一篇 2016-08-18 10:10

相关推荐

  • socket阻塞与非阻塞,同步与异步、I/O模型

    1. 概念理解      在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式:同步:      所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回。也就是必须一件一件事做,等前…

    Linux干货 2015-04-10
  • 如何使用openssl工具创建私有CA

    一、CA及证书 非对称加密是为了保证互联网中通讯信息安全使用的一种算法,密钥是成对出现(公钥和私钥),它的特点是发送方A使用接收方B的公钥加密数据,所有只有B拥有与之配对的私钥解密该数据,反之亦然。那么,A和B之间怎么交换得到对方的真实安全的公钥呢?此时就需要一个权威的机构来验证公钥的合法性,这个机构称之为CA(Certification Authority…

    2017-07-16
  • 第三周博客作业

    who useradd usermod groupadd

    Linux干货 2017-12-17
  • N25 第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [jizo@jizo ~]$ who | cut -d" " -f1 | uniq jizo  2、取出最后登录到当前系统的用户的相关信息。 [root@jizo ~]# who | tail -l jizo     …

    Linux干货 2016-12-18
  • 文件查找与压缩

    文件查找与压缩   Locate:非事实查找(数据库查找)     查询系统上预建的文件索引数据库 /var/lib/mlocate/mlocate.db 依赖于事先构建的索引; 索引的构建是在系统较为空闲时自动进行(周期性任务); 管理 员手动更新数据库(updatedb);   工作特点: 查找…

    Linux干货 2016-08-18
  • 第六周作业-练习题sed,crontab,bash

    1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;    方法一用sed:          方法二用vim       &…

    Linux干货 2017-01-16