shell脚本编程的例子(55例子)-2

21-31的shell实例脚本,这里粘贴的都是经过本人的实际实验环境调试实现的,仅供参考。

Eg21、关于for-loop_and_break的运用。

#!/bin/bash

## filename: for-loop_and_break.sh

i=1

for day in Mon Tue Wed Thu Fri

do

   echo "Weekday $((i++)) : $day"

   if [ $i -eq 3 ]; then

      break

   fi

done

765e6a74a66149aab45ff34de7ae5777.png

 

Eg22、关于for-loop_and_continute的运用。

 

#!/bin/bash

## filename: for-loop_and_continue.sh

i=1

for day in Mon Tue Wed Thu Fri Sat Sun

do

   echo -n "Day $((i++)) : $day"

   if [ $i -eq 7 -o $i -eq 8 ]; then

       echo " (WEEKEND)"

       continue

   fi

   echo " (weekday)"

done

 

#set -x

msg="This is a test massage."

for name in `cat mail_list` ; do

  if [[ $name == boy || $name == "dx1a" ]]

  then    continue

  else    mail $name <<< "$msg"

  fi

Done

 

36007364e8b747759ed284a5fd105c8b.png

 

68149d592b1948a693da128a8aa77c9f.png

测试失败。但是以及达到跳出continue这个了。

注意:

1、需要安装mailutils和ssmtp:apt install mailutils  && apt install ssmtp;

同时需要配置ssmtp.conf 和revealiases文件以及对应的邮箱参数。

2、<<< 是用于输入单行文本的重定向符号。它的一般用法是:

command <<< "text"

在这个语法中,command 是要执行的命令,"text" 是要输入的单行文本。<<< 将会将"text"的内容传递给command作为标准输入。 例如: echo <<< "Hello, World!"

因此,<< 用于输入多行文本,而 <<< 用于输入单行文本,要加引号"才算一行。这两个符号在shell脚本中经常用于向命令传递输入。

现在修改配置文件:

a、修改ssmtp.conf

a1a9293af5564bb69821e1787bb66ad8.png

b、修改mail_list内容

82c2843815a84b7489390d6f71b4fa7b.png

再运行则提示成功:

8c50a044dfc8420098bcd4a589e585d4.png

 

Eg23、关于for-c的运用(c语言实现计数循环)。

#!/bin/bash

## filename: for--C-style.sh

for ((i=0;i<10;i++)) ; do echo $i; done

for (( i=1; i <= 10; i++ ))

do

    echo "Random number $i: $RANDOM"

done

for ((i=1, j=10; i <= 5 ; i++, j=j+5)) ; do

    echo "Number $i: $j"

done

b673c7a091b140dabdba350ac1335238.png

 

Eg24、关于for-c的运用(c语言实现求和运算)。

#!/bin/bash

## filename: for--C-style_sum.sh

s=0

for ((i=1;i<=100;i++)) ; do let s=$s+$i ; done

echo sum\(1..100\)=$s

for ((s=0,i=1;i<=100;i++)) ; do ((s+=i)) ; done

echo sum\(1..100\)=$s

for ((s=0,i=1;i<=100;s+=i,i++))

do

   :  # 空语句

done

# for ((s=0,i=1;i<=100;s+=i,i++)) ; do : ; done

echo sum\(1..100\)=$s

a32b421a65964c56b3747b2a01357a95.png

 

Eg25.关于for-c的运用(批量添加用户)

#!/bin/bash

## filename: addusers_for_C-style.sh

# 成批添加10个用户

for (( n=1; n<=10; n++ ))

do

    if ((n<10))

    then  st="st0${n}"

    else  st="st${n}"

    fi

    useradd $st

    echo "centos"|passwd --stdin $st

    chage -d 0 $st

done

这里没有在本机运行,之前sy6中以及验证,这里就不添加用户的实例操作了。

不过注意:

8e3a264067284e62b1b3234372ade059.png

请童鞋自己验证!

Eg25、while语句实例-猜数字

#!/bin/bash

## filename: while--guess_number.sh

# $RANDOM是一个系统随机数的环境变量,模100运算用于生成1-100的随机整数

num=$((RANDOM%100))

# 使用永真循环、条件退出(break)的方式接收用户的猜测并进行判断

while :

do

  read  -p  "Please guess my number [0-99]: "  answer

  if   [ $answer -lt $num ]

  then echo "The number you inputed is less then my NUMBER."

  elif [[ $answer -gt $num ]]

  then echo "The number you inputed is greater then my NUMBER."

  elif ((answer==num))

  then echo "Bingo! Congratulate: my NUMBER is $num." ; break

  fi

done

b3dcf42cb849458a8e2ff9827f65d164.png

 

Eg26、while语句实例-输入重定向读文件

#!/bin/bash

## filename: while--read_file.sh

file=/etc/resolv.conf

while IFS= read -r line

do

    # echo line is stored in $line

    echo $line

done < "$file"

while IFS=: read -r user enpass uid gid desc home shell

do

    # only display if UID >= 500

    [ $uid -ge 500 ] && echo "User $user ($uid) assigned \"$home\" home directory with $shell shell."

done < /etc/passwd

6c560eb54f75467595357f8f8186a9eb.png

这个例子自己理解下。

Eg27、while实例-替换有空格的文件名,改为下划线

 

使用管道为 while 传递输入

#!/bin/bash

## filename: while-rename_filename.sh

# 找出当前目录下包含空格的文件名,将空格替换为下划线

DIR="."

find $DIR -type f | while read file; do

  # using POSIX class [:space:] to find space in the filename

  if [[ "$file" = *[[:space:]]* ]]; then

     # substitute space with "_" character (rename the filename)

     mv "$file" $(echo $file | tr ' ' '_')

  fi

done

b0707d37b8fa41839168b80060a59c6b.png

 

Eg28、while循环实例-

#!/bin/bash

## filename: /etc/cron.daily/monitor_disk_space.cron

# set admin email so that you can get email

ADMIN="xh_ry@163.com"

# set alert level 90% is default

ALERT=90

LANG=C

df -PH|egrep -v '^Filesystem|tmpfs|cdrom'|awk '{ print $5 " " $1 }' | while read output;

do

  usep=$(echo $output | awk '{ print $1 }' | cut -d'%' -f1  )

  partition=$(echo $output | awk '{ print $2 }' )

  if [ $usep -ge $ALERT ]; then

    ( echo -n "Running out of space "

      echo -n \"$partition ($usep%)\"

      echo -n " on \"$(hostname)\" as on \"$(date)\"."

    )| mail -s "Alert: Almost out of disk space $usep" $ADMIN

  fi

Done

这里直接执行了就会一直发送给xh_ry@163.com 的邮件了。

367656b179de4268b6539edcbb3ac70e.png

 

2d0d8deddd604ea584294eb904efd22d.png

所以要中止,需要关闭linux或者killall 28eg.sh

Eg29、until实例-host-online

#!/bin/bash

## filename: until-host_online_to_ssh.sh

read -p "Enter IP Address:" ipadd

echo $ipadd

until ping -c 1 $ipadd &> /dev/null

do

      sleep 60

done

ssh $ipadd

7b678f65d5914057b19933af97b6f998.png

 

Eg30、

#!/bin/bash

## filename: until-user_online_to_write.sh

username=$1

if [ $# -lt 1 ] ; then

  echo "Usage: `basename $0`  <username>  [<message>]"

  exit 1

fi

if grep "^$username:" /etc/passwd > /dev/null ; then   :

else

  echo "$username is not a user on this system."

  exit 2

fi

until who|grep "$username" > /dev/null ; do

    echo "$username is not logged on."

    sleep 6

done

shift ; msg=$*

[[ X"$msg" == "X" ]] && msg="Hello, $username"

echo "$msg" | write $username

633474eb52844c4d86c2689fdc84608a.png

这个例子的情况不是很好,后续会更新。

Eg31、while-until-for综合实例

#!/bin/bash

## filename: while-until-for_sum.sh

# 使用当型循环求 sum(1..100)

((i=0,s=0))        # i=0 ; s=0

while ((i<100)) ; do ((i++,s+=i)) ; done

echo sum\(1..100\)=$s

# 使用直到型循环求 sum(1..100)

((i=0,s=0))

until ((i==100)) ; do ((i++,s+=i)) ; done

echo sum\(1..100\)=$s

# 使用C风格的 for 循环求 sum(1..100)

for ((s=0,i=1;i<=100;s+=i,i++)) ; do : ; done

echo sum\(1..100\)=$s

1dd429c0e5704f1baa9a1aa5e0fb46af.png

 

-----------shell55例脚本分隔符----------------

 

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个在高校打杂的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值