【shell第4天】shell循环练习

1.9 * 9 乘法表,for列表循环,for循环(c语言风格), while循环

#!/bin/bash
echo "**********for列表循环*************"
for i in {1..9}
do
    for j in $(seq 1 $i)
    do
        echo -e -n "$j * $i = $[ i * j ]\t"
    done
    echo ''
done
echo "**********C风格for循环*************"
for ((i=1;i<=9;i++))
do
    for ((j=1;j<=i;j++))
    do
        echo -e -n "$j * $i = $[ i * j ]\t"
    done
    echo ''
done
echo "**********while循环*************"
i=1
while [ $i -le 9 ]
do
    j=1
    while [ $j -le $i ]
    do
        echo -e -n "$j * $i = $[ i * j ]\t"
        let j=j+1
    done
    let i=i+1
    echo ''
done

执行结果:
[mmrrj@localhost 4day]$ bash for_99.sh 
**********for列表循环*************
1 * 1 = 1
1 * 2 = 2       2 * 2 = 4
1 * 3 = 3       2 * 3 = 6       3 * 3 = 9
1 * 4 = 4       2 * 4 = 8       3 * 4 = 12      4 * 4 = 16
1 * 5 = 5       2 * 5 = 10      3 * 5 = 15      4 * 5 = 20      5 * 5 = 25
1 * 6 = 6       2 * 6 = 12      3 * 6 = 18      4 * 6 = 24      5 * 6 = 30      6 * 6 = 36
1 * 7 = 7       2 * 7 = 14      3 * 7 = 21      4 * 7 = 28      5 * 7 = 35      6 * 7 = 42      7 * 7 = 49
1 * 8 = 8       2 * 8 = 16      3 * 8 = 24      4 * 8 = 32      5 * 8 = 40      6 * 8 = 48      7 * 8 = 56      8 * 8 = 64
1 * 9 = 9       2 * 9 = 18      3 * 9 = 27      4 * 9 = 36      5 * 9 = 45      6 * 9 = 54      7 * 9 = 63      8 * 9 = 72      9 * 9 = 81
**********C风格for循环*************
1 * 1 = 1
1 * 2 = 2       2 * 2 = 4
1 * 3 = 3       2 * 3 = 6       3 * 3 = 9
1 * 4 = 4       2 * 4 = 8       3 * 4 = 12      4 * 4 = 16
1 * 5 = 5       2 * 5 = 10      3 * 5 = 15      4 * 5 = 20      5 * 5 = 25
1 * 6 = 6       2 * 6 = 12      3 * 6 = 18      4 * 6 = 24      5 * 6 = 30      6 * 6 = 36
1 * 7 = 7       2 * 7 = 14      3 * 7 = 21      4 * 7 = 28      5 * 7 = 35      6 * 7 = 42      7 * 7 = 49
1 * 8 = 8       2 * 8 = 16      3 * 8 = 24      4 * 8 = 32      5 * 8 = 40      6 * 8 = 48      7 * 8 = 56      8 * 8 = 64
1 * 9 = 9       2 * 9 = 18      3 * 9 = 27      4 * 9 = 36      5 * 9 = 45      6 * 9 = 54      7 * 9 = 63      8 * 9 = 72      9 * 9 = 81
**********while循环*************
1 * 1 = 1
1 * 2 = 2       2 * 2 = 4
1 * 3 = 3       2 * 3 = 6       3 * 3 = 9
1 * 4 = 4       2 * 4 = 8       3 * 4 = 12      4 * 4 = 16
1 * 5 = 5       2 * 5 = 10      3 * 5 = 15      4 * 5 = 20      5 * 5 = 25
1 * 6 = 6       2 * 6 = 12      3 * 6 = 18      4 * 6 = 24      5 * 6 = 30      6 * 6 = 36
1 * 7 = 7       2 * 7 = 14      3 * 7 = 21      4 * 7 = 28      5 * 7 = 35      6 * 7 = 42      7 * 7 = 49
1 * 8 = 8       2 * 8 = 16      3 * 8 = 24      4 * 8 = 32      5 * 8 = 40      6 * 8 = 48      7 * 8 = 56      8 * 8 = 64
1 * 9 = 9       2 * 9 = 18      3 * 9 = 27      4 * 9 = 36      5 * 9 = 45      6 * 9 = 54      7 * 9 = 63      8 * 9 = 72      9 * 9 = 81
[mmrrj@localhost 4day]$ 

2.使用for循环创建30个用户: test01~test30, 并设置密码为test01123456~test30123456

#!/bin/bash
for user in test{01..30}
do
    if ! id -u $user &> /dev/null 
    then
        useradd $user 
        echo "$user"123456 | passwd --stdin $user &> /dev/null 
    else
        echo "$user is exists..." 
    fi
done

执行结果:
[mmrrj@localhost 4day]$ sudo bash add_user.sh 
[mmrrj@localhost 4day]$ cat /etc/passwd |grep test
test01:x:1001:1001::/home/test01:/bin/bash
test02:x:1002:1002::/home/test02:/bin/bash
test03:x:1003:1003::/home/test03:/bin/bash
test04:x:1004:1004::/home/test04:/bin/bash
test05:x:1005:1005::/home/test05:/bin/bash
test06:x:1006:1006::/home/test06:/bin/bash
test07:x:1007:1007::/home/test07:/bin/bash
test08:x:1008:1008::/home/test08:/bin/bash
test09:x:1009:1009::/home/test09:/bin/bash
test10:x:1010:1010::/home/test10:/bin/bash
test11:x:1011:1011::/home/test11:/bin/bash
test12:x:1012:1012::/home/test12:/bin/bash
test13:x:1013:1013::/home/test13:/bin/bash
test14:x:1014:1014::/home/test14:/bin/bash
test15:x:1015:1015::/home/test15:/bin/bash
test16:x:1016:1016::/home/test16:/bin/bash
test17:x:1017:1017::/home/test17:/bin/bash
test18:x:1018:1018::/home/test18:/bin/bash
test19:x:1019:1019::/home/test19:/bin/bash
test20:x:1020:1020::/home/test20:/bin/bash
test21:x:1021:1021::/home/test21:/bin/bash
test22:x:1022:1022::/home/test22:/bin/bash
test23:x:1023:1023::/home/test23:/bin/bash
test24:x:1024:1024::/home/test24:/bin/bash
test25:x:1025:1025::/home/test25:/bin/bash
test26:x:1026:1026::/home/test26:/bin/bash
test27:x:1027:1027::/home/test27:/bin/bash
test28:x:1028:1028::/home/test28:/bin/bash
test29:x:1029:1029::/home/test29:/bin/bash
test30:x:1030:1030::/home/test30:/bin/bash
[mmrrj@localhost 4day]$ su test01
密码: test01123456
[test01@localhost 4day]$ 

3.使用循环去判断网段内的IP(1~254),本机除外,可以ping通的使用 ssh远程登录

#!/bin/bash
self=192.168.10.128
name=mmrrj
for host in 192.168.10.{127..132}
do
    ping -c 2 -W 1 "$host" &> /dev/null 
    if [ $? -eq 0 ];then 
        echo host "$host" is running
        if [ $host != $self ]
        then
            echo "ifconfig |grep inet " |ssh $name@$host
        fi
    else
        echo host "$host" is not running 
    fi
done

执行结果:
[mmrrj@localhost 4day]$ bash ping_and_ssh.sh 
host 192.168.10.127 is not running
host 192.168.10.128 is running
host 192.168.10.129 is not running
host 192.168.10.130 is not running
host 192.168.10.131 is running
Pseudo-terminal will not be allocated because stdin is not a terminal.
client_global_hostkeys_private_confirm: server gave bad signature for RSA key 0: error in libcrypto
Activate the web console with: systemctl enable --now cockpit.socket

        inet 192.168.10.131  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fecf:6e6d  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::21f1:7d4b:d0d2:74cd  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
host 192.168.10.132 is not running
[mmrrj@localhost 4day]$ 

4.使用$@和$*作为for循环的列表,并体现出区别

#!/bin/bash
echo '-----$@-----'
for i in "$@"
do
    echo "\"$i\""
done
echo '-----$*-----'
for i in "$*"
do
    echo "\"$i\""
done

执行结果:
[mmrrj@localhost 4day]$ bash args.sh 1 2 3
-----$@-----
"1"
"2"
"3"
-----$*-----
"1 2 3"

5.使用循环去读取文件内容并输出: 3种方式(1.exec+while循环 2.管道符+while循环 3.重定向+while)

#!/bin/bash
echo "-----exec+while-----"
exec < file
while read line
do
    echo "$line"
done
echo "-----管道符+while"
cat file | while read line
do
    echo "$line"
done
echo "-----重定向+while"
while read line 
do
    echo "$line"
done < file

执行结果:
[mmrrj@localhost 4day]$ bash read_file.sh 
-----exec+while-----
this
is
a
file
-----管道符+while
this
is
a
file
-----重定向+while
this
is
a
file
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值