欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

马哥 13_01 _bash脚本编程之九 while循环 有大用

image.png





while,until,for

 break:退了循环

 continue:中断当前循环(本轮循环),进入下一轮循环

image.png


1000以内所有整数的和,SUM大于5000时就不再加了



while 的特殊用法一:

while : ; do  (冒号表示 死循环 相当于 一直为true吧 永远为真,永远执行 )



done

while 的特殊用法二:

while read LINE; do               (读取文件,每读取一行,把变量放到LINE中)


done    <  /PATH/TO/SOMEFILE        (输入重定向给它一个文件路径)



/etc/passwd 中哪个用户的默认shell 为 bash shell,如果是,就显示用户名,否则不用显示




image.png


./syntax.sh a.sh


bash -n $1 执行状态为0表示脚本没有语法错误,否则就是有语法错误,就用vim打开


until bash -n $1 &> /dev/null ; do

  read -p "syntax error, [Qq] to quit, others for editing:"  CHOICE

 case $CHOICE in

    q|Q)

            echo "Something wrong,quiting."

            exit 5 ;;

    *)

            vim + $1;;

esac


done



(vim + $1 加号表示启动后调到文件末尾)












:.,$s@/etc/yum.repos.d/$1@$REPOFILE@g  在末行模式下 替换


[root@ebs-22618 ~]# vim mkrepo.sh

#!/bin/bash

#


REPOFILE=/etc/yum.repos.d/$1

if [ -e $1 ]; then

  echo "$1 exists."

  exit 3

fi


read -p "Repository ID: " REPOID

until [ $REPOID == 'quit' ]; do

  echo "[$REPOID]" >> $REPOFILE

  read -p "Repository name: " REPONAME

  echo "name=$EPONAME" >> $REPOFILE

  read -p "Repository Baseurl: " REPOURL

  echo "baseurl=$REPOURL" >> $REPOFILE

  echo -e 'enbaled=1\ngpgcheck=0' >> $REPOFILE

  read -p "Repository ID: " REPOID

done


image.png

[root@ebs-22618 ~]# chmod +x mkrepo.sh

[root@ebs-22618 ~]# ./mkrepo.sh hello.repo

Repository ID: hello

Repository name: hello world

Repository Baseurl: http://172.16.0.1/yum/Server

Repository ID: quit

image.png



[root@ebs-22618 ~]# cd /etc/yum.repos.d/

[root@ebs-22618 yum.repos.d]# ls

CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo

CentOS-Debuginfo.repo  CentOS-Media.repo      hello.repo

[root@ebs-22618 yum.repos.d]#



[root@ebs-22618 ~]# vim odd2.sh

#!/bin/bash

#

let SUM=0

let I=0


while [ $I -lt 100 ]; do

  let I++

  if [ $[$I%2] -eq 0 ]; then

     continue

  fi


  let SUM+=$I

  done


  echo $SUM

~





[root@ebs-22618 ~]# chmod +x odd.sh

[root@ebs-22618 ~]# ./odd.sh

2500

[root@ebs-22618 ~]#


#!/bin/bash

#

let SUM=0

let I=0


while [ $I -le 100 ]; do

  let I++

  if [ $[$I%2] -eq 0 ]; then

     continue

  fi


  let SUM+=$I

  done


  echo $SUM

image.png



[root@ebs-22618 ~]# chmod +x odd2.sh

[root@ebs-22618 ~]# ./odd2.sh

2500



[root@ebs-22618 ~]# vim sum2.sh

#!/bin/bash

#

declare -i SUM=0

for I in {1..1000};do

  let SUM+=$I

  if [ $SUM -gt 5000 ];then

    break

  fi

done

echo $I

echo $SUM

~

image.png

[root@ebs-22618 ~]# chmod +x sum2.sh

[root@ebs-22618 ~]# ./sum2.sh

100

5050



[root@ebs-22618 ~]# vim sum2.sh

#!/bin/bash

#

declare -i SUM=0

for I in {1..1000};do

  let SUM+=$I

  if [ $SUM -gt 10000 ];then

    break

  fi

done

echo $I

echo $SUM

~

image.png



[root@ebs-22618 ~]# ./sum2.sh

141

10011

[root@ebs-22618 ~]#




# vim showfile.sh

#!/bin/bash

#

while :; do                # 死循环 

 read -p "File path: " FILEPATH

 if [ -e $FILEPATH ]; then

  echo "$FILEPATH exists."

  else

    echo "No $FILEPATH."

 fi

done

image.png


[root@ebs-22618 ~]# chmod +x showfile.sh


[root@ebs-22618 ~]# ./showfile.sh

File path: /etc/inittab

/etc/inittab exists.

File path: hello

No hello.

File path: /etc/issue

/etc/issue exists.

File path: ^C                    (死循环只能 ctrl+x 终止了)

[root@ebs-22618 ~]#


image.png



# vim showfile.sh

#!/bin/bash

#

while :; do

 read -p "File path: " FILEPATH

 [ $FILEPATH == 'quit' ] && break

 if [ -e $FILEPATH ]; then

  echo "$FILEPATH exists."

  else

    echo "No $FILEPATH."

 fi

done


echo "Quit."

image.png


[root@ebs-22618 ~]# ./showfile.sh

File path: hello

No hello.

File path: /etc/inittab

/etc/inittab exists.

File path: quit

Quit.

[root@ebs-22618 ~]#

image.png





[root@ebs-22618 ~]# vim showusers.sh

#!/bin/bash

#

FILE=/etc/passwd


while read LINE; do

 [ `echo $LINE | awk -F : '{print $7}'` == '/bin/bash' ] && echo $LINE | awk -F : '{print $1}'

done < $FILE

image.png

# chmod +x  showusers.sh

[root@ebs-22618 ~]# ./showusers.sh

root

hadoop

[root@ebs-22618 ~]#

image.png

image.png

这里还要断第{print $7} 是不是为空



# ./showusers.sh | head -6          (只显示前6个用户)

image.png


# vim showusers.sh 

#!/bin/bash

#

FILE=/etc/passwd

let I=0


while read LINE; do

 [ `echo $LINE | awk -F : '{print $7}'` == '/bin/bash' ] && echo $LINE | awk -F : '{print $1}' && let I++

 [ $I -ge 6 ] && break

done < $FILE

image.png



当 id 号 小于等于505时,就不显示 用户名

[root@ebs-22618 ~]# vim showusers.sh

#!/bin/bash

#

FILE=/etc/passwd

let I=0


while read LINE; do

 [ `echo $LINE | awk -F : '{print $3}'` -le 505 ] && continue

 [ `echo $LINE | awk -F : '{print $7}'` == '/bin/bash' ] && echo $LINE | awk -F : '{print $1}' && let I++

 [ $I -ge 6 ] && break

done < $FILE

~

~

image.png



[root@ebs-22618 ~]# ./showusers.sh

hadoop

[root@ebs-22618 ~]#





[root@ebs-22618 ~]# vim syntax.sh

#!/bin/bash

#

until bash -n $1 &> /dev/null ; do

read -p "syntax error, [Qq] to quit, others for editing:" CHOICE

 case $CHOICE in

 q|Q)

 echo "Something wrong,quiting."

 exit 5 ;;

 *)

  vim + $1;;

 esac


done

~

image.png


[root@ebs-22618 ~]# vim hello.sh

#!/bin/bash

#

if [ $A-eq 0];then

  echo "hello

~

~




[root@ebs-22618 ~]# chmod +x syntax.sh


[root@ebs-22618 ~]# ./syntax.sh hello.sh

image.png

普通分类: