shell脚本 for循环基本使用

目录

for循环的语法结构

for列表的定义 批量创建用户

批量删除用户

使用for循环ping用户要访问的主机

循环规则

乘法表脚本

while随机循环

猜数字脚本

 case控制服务的基本应用

简单的识别

使用case写一个控制vsftpd服务脚本

批量创建文件脚本


for循环

for循环的语法结构

for 变量名 in 取值列表

do

命令序列

done

for列表的定义 批量创建用户

[root@localhost ~]# vim q.txt              定义用户名

bom

mmm

ccc

[root@localhost ~]# vim dd.sh            编写脚本

#!/bin/bash

for dd in $(cat q.txt)                              读取文件 取值

do

useradd $dd                                          调用

echo "123456" | passwd --stdin $dd   密码设置给用户

done                                                       结束语句

[root@localhost ~]# sh dd.sh              执行

useradd:用户“bom”已存在

更改用户 bom 的密码 。

passwd:所有的身份验证令牌已经成功更新。

useradd:用户“mmm”已存在

更改用户 mmm 的密码 。

passwd:所有的身份验证令牌已经成功更新。

useradd:用户“ccc”已存在

更改用户 ccc 的密码 。

passwd:所有的身份验证令牌已经成功更新。

批量删除用户

[root@localhost ~]# vim dd.sh

#!/bin/bash

for dd in $(cat q.txt)                   

do

userdel -r $dd                                 只需要修改成     del

Done

使用for循环ping用户要访问的主机

[root@localhost ~]# vim q.txt       配置取值文件

192.168.1.139                                  本机
192.168.1.111                                  随便设置的
192.168.1.121                                  随便设置的

[root@localhost ~]# vim dd.sh     编写脚本

#!/bin/bash

for ip in $(cat q.txt)                        变量名设置  取值列表

do

ping -c 3 -i 0.2 -W 3 $ip &>/dev/null       设置-c ping三次 -i  0.2秒一次  -W超过三秒定义ping不通                          &>/dev/null    把输出信息到一个黑洞里                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

if [ $? -eq 0 ]        意思是,如果上一个命令执行成功(退出状态码为0),则执行条件语句中的代码块

then

echo "host $ip is up"                成功up          

else

echo "host $ip is down"           失败down

fi

done

[root@localhost ~]# sh dd.sh           启动脚本

host 192.168.1.139 is up

host 192.168.1.111 is down

host 192.168.1.121 is down

循环规则

[root@localhost ~]# for i in {10..1};do echo $i;sleep 1;done    这段代码是一个简单的倒计时程序。它使用了一个循环来从10倒数到1,并在每个数字之间暂停1秒钟。每次循环,它会打印当前的数字。这段代码可以用于创建一个简单的倒计时效果。

10

9

8

7

6

5

4

3

2

1

[root@localhost ~]# vim dd.sh

#!/bin/bash                               这段代码是一个嵌套的循环,用于输出1到9的所有组合

for i in {1..9}                             外层循环使用变量i,从1到9依次取值。

do

        for j in {1..9}                    内层循环使用变量j,也从1到9依次取值。

        do

                echo $i $j

        done

done

每次循环,都会输出当前的i和j的值。所以,这段代码会输出1 1、1 2、1 3...直到9 9的所有组合。

[root@localhost ~]# sh dd.sh

1 1

1 2

1 3

1 4

1 5

1 6

1 7

1 8

1 9

2 1

2 2

2 3

2 4

2 5

2 6

2 7

2 8

。。。。。。。

乘法表脚本

[root@localhost ~]# vim dd.sh

#!/bin/bash

for i in {1..9}

do

for ((j=1;j<=$i;j++))

do

echo -n "${j}x${i}=$(($i*$j))"

done

echo

done

[root@localhost ~]# sh dd.sh

1x1=1

1x2=22x2=4

1x3=32x3=63x3=9

1x4=42x4=83x4=124x4=16

1x5=52x5=103x5=154x5=205x5=25

1x6=62x6=123x6=184x6=245x6=306x6=36

1x7=72x7=143x7=214x7=285x7=356x7=427x7=49

1x8=82x8=163x8=244x8=325x8=406x8=487x8=568x8=64

1x9=92x9=183x9=274x9=365x9=456x9=547x9=638x9=729x9=81

while随机循环

while的命令结构

while [条件]

do

命令序列

done

随机创建20个账户密码设置为123456

[root@localhost ~]# vim dd.sh

#!/bin/bash

i=1

while  [ $i -le 20 ]

do

        #userdel -r stu$i

        useradd stu$i

        echo "123456" | passwd --stdin stu$i

        let i++

done

[root@localhost ~]# sh dd.sh

更改用户 stu1 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu2 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu3 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu4 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu5 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu6 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu7 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu8 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu9 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu10 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu11 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu12 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu13 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu14 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu15 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu16 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu17 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu18 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu19 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 stu20 的密码 。

passwd:所有的身份验证令牌已经成功更新。

[root@localhost ~]# cat /etc/passwd

stu1:x:1001:1001::/home/stu1:/bin/bash

stu2:x:1002:1002::/home/stu2:/bin/bash

stu3:x:1003:1003::/home/stu3:/bin/bash

stu4:x:1004:1004::/home/stu4:/bin/bash

stu5:x:1005:1005::/home/stu5:/bin/bash

stu6:x:1006:1006::/home/stu6:/bin/bash

stu7:x:1007:1007::/home/stu7:/bin/bash

stu8:x:1008:1008::/home/stu8:/bin/bash

stu9:x:1009:1009::/home/stu9:/bin/bash

stu10:x:1010:1010::/home/stu10:/bin/bash

stu11:x:1011:1011::/home/stu11:/bin/bash

stu12:x:1012:1012::/home/stu12:/bin/bash

stu13:x:1013:1013::/home/stu13:/bin/bash

stu14:x:1014:1014::/home/stu14:/bin/bash

stu15:x:1015:1015::/home/stu15:/bin/bash

stu16:x:1016:1016::/home/stu16:/bin/bash

stu17:x:1017:1017::/home/stu17:/bin/bash

stu18:x:1018:1018::/home/stu18:/bin/bash

stu19:x:1019:1019::/home/stu19:/bin/bash

stu20:x:1020:1020::/home/stu20:/bin/bash

猜数字脚本

[root@localhost ~]# vim dd.sh

#!/bin/bash

echo "这是猜商品价格(1-1000)的小游戏,猜猜看"

pc=$(expr $RANDOM % 1000 + 1)

cs=0

while true

do

read -p "输入你猜测的价格:" int

let cs++

if [ $int -gt $pc ]

then

echo "价格高了"

elif [ $int -eq $pc ]

then

echo "恭喜你猜对啦"

echo "共猜测了$cs次"

exit

else

echo "价格低了"

fi

done

[root@localhost ~]# sh dd.sh

这是猜商品价格(1-1000)的小游戏,猜猜看

输入你猜测的价格:500

价格高了

输入你猜测的价格:400

价格高了

输入你猜测的价格:300

价格高了

输入你猜测的价格:200

价格高了

输入你猜测的价格:100

价格低了

输入你猜测的价格:150

价格高了

输入你猜测的价格:133

价格低了

输入你猜测的价格:135

价格低了

输入你猜测的价格:140

价格低了

输入你猜测的价格:145

价格高了

输入你猜测的价格:143

恭喜你猜对啦

共猜测了11次

 case控制服务的基本应用

case的语法格式

case 变量值 in

模式1)

;;

模式2)

;;

*)

默认命令序列

esac

简单的识别

[root@localhost ~]# vim dd.sh

#!/bin/bash

read -p "请输入一个字符:" str

case $str in

[a-zA-Z])

echo "字母"

;;

[0-9])

echo "数字"

;;

*)

echo "特殊符号"

esac

[root@localhost ~]# sh dd.sh

请输入一个字符:1

数字

[root@localhost ~]# sh dd.sh

请输入一个字符:w

字母

[root@localhost ~]# sh dd.sh

请输入一个字符:!

特殊符号

使用case写一个控制vsftpd服务脚本

[root@localhost ~]# vim dd.sh

#!/bin/bash

#chkconfig:35 80 21

#Description:vsftpd Server

case "$1" in

start)

echo "正在启动vsftpd服务[确定]"

;;

stop)

echo "正在停止vsftpd服务[确定]"

;;

restart)

echo "正在重新启动vsftpd服务[确定]"

;;

*)

echo "用法:$0{start|stop|restart}"

esac

[root@localhost ~]# sh dd.sh

用法:dd.sh{start|stop|restart}

[root@localhost ~]# sh dd.sh start

正在启动vsftpd服务[确定]

[root@localhost ~]# sh dd.sh stop

正在停止vsftpd服务[确定]

[root@localhost ~]# sh dd.sh restart

正在重新启动vsftpd服务[确定]

[root@localhost ~]# sh dd.sh

用法:dd.sh{create|delete|list}

批量创建文件脚本

[root@localhost ~]# vim dd.sh

#!/bin/bash

case $1 in

create)

        touch /tmp/{1..100}.txt

;;

delete)

        rm -rf /tmp/{1..100}.txt

;;

list)

        ls -l /tmp/{1..100}.txt

;;

*)

        echo "用法:$0{create|delete|list}"

esac

~      

[root@localhost ~]# sh dd.sh create

[root@localhost ~]# ls /tmp

100.txt  33.txt  57.txt  80.txt

10.txt   34.txt  58.txt  81.txt

11.txt   35.txt  59.txt  82.txt

12.txt   36.txt  5.txt   83.txt

13.txt   37.txt  60.txt  84.txt

14.txt   38.txt  61.txt  85.txt

15.txt   39.txt  62.txt  86.txt

16.txt   3.txt   63.txt  87.txt

17.txt   40.txt  64.txt  88.txt

18.txt   41.txt  65.txt  89.txt

19.txt   42.txt  66.txt  8.txt

1.txt    43.txt  67.txt  90.txt

20.txt   44.txt  68.txt  91.txt

21.txt   45.txt  69.txt  92.txt

22.txt   46.txt  6.txt   93.txt

23.txt   47.txt  70.txt  94.txt

24.txt   48.txt  71.txt  95.txt

25.txt   49.txt  72.txt  96.txt

26.txt   4.txt   73.txt  97.txt

27.txt   50.txt  74.txt  98.txt

28.txt   51.txt  75.txt  99.txt

29.txt   52.txt  76.txt  9.txt

2.txt    53.txt  77.txt  systemd-private-c83019d224504701af71956e9438e8ac-chronyd.service-hM2AIb

30.txt   54.txt  78.txt

31.txt   55.txt  79.txt

32.txt   56.txt  7.txt

[root@localhost ~]# sh dd.sh delete

[root@localhost ~]# sh dd.sh list

ls: 无法访问/tmp/1.txt: 没有那个文件或目录

ls: 无法访问/tmp/2.txt: 没有那个文件或目录

ls: 无法访问/tmp/3.txt: 没有那个文件或目录

ls: 无法访问/tmp/4.txt: 没有那个文件或目录

ls: 无法访问/tmp/5.txt: 没有那个文件或目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值