【实验】一百个经典shell脚本(6-10)

发布于:2023-01-04 ⋅ 阅读:(315) ⋅ 点赞:(0)

脚本六

9 * 9 乘法表,for列表循环,for循环(c语言风格), while循环、可选单层循环

#!/bin/bash
############################################################
# File Name: x_99.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-19 19:37:38
# Description:
############################################################
for i in 1 2 3 4 5 6 7 8 9
 do
 for j in 1 2 3 4 5 6 7 8 9
  do
    if [ $i -ge $j ];then
    echo -n  "$j*$i=$((i*j)) "
    fi
  done#  echo -e "\n"
  echo -e "\n"
 done 

var_i=1
while [ $var_i -lt 10 ]
do
  var_j=1
  while [ $var_j -le $var_i ]
  do
     echo -n "$var_j * $var_i = $((i*j))    "   
     let var_j++
  done
   let var_i++
   echo -e "\n"
done

99

脚本七

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

#!/bin/bash
############################################################
# File Name: circulate_add_host.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-21 18:00:09
# Description:
############################################################
for var_i in {1..30}
do
    var_host=`printf "%02g" $var_i`
#    echo $var_host
    if ! id -u $var_host &> /dev/null
    then
        useradd $var_host
        echo "$var_host 122547" | passwd --stdin $var_host &> /dev/null
    else
        echo "$var_host is exits!"
    fi
done

用户

脚本八

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

#!/bin/bash
############################################################
# File Name: ping_test_ssh.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-21 18:16:33
# Description:
############################################################
for var_hosts in 192.168.186.{1..254}
do
    ping -c 2 -i 0.3 -W 1 $var_hosts &> /dev/null
    if [ $? -ne 0 ]
    then
        echo "the $var_hosts is die"
    else
        echo "the $var_hosts is active"
        ssh root@$var_hosts
    fi
done

在这里插入图片描述

脚本九

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

#!/bin/bash
############################################################
# File Name: test_@*.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-19 20:12:30
# Description:
############################################################
for i in "$@"
do
echo "$i"
done

for i in "$*"
do
echo "$i"
done

测试

脚本十

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

#!/bin/bash
############################################################
# File Name: read_file.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-21 21:31:25
# Description:
############################################################
exec < test_txt
while read var_file
do
    echo $var_file
done

while read var_txt
do
    echo $var_txt
done < test_txt

cat test_txt | while read var_line
do
    echo $var_line
done

读取文件