shell的循环运用

实验1:1.先去打印每一个字符串:以及它的长度
             2.然后将长度小于6的单词,拼接成一个字符串:用-来进行连接
             3.在for循环结束之后输出

[root@localhost ~]# vim length_base.sh
data=""
for i in rabbit is favorite to eat cabbage
do
   echo "$i length is ${#i}"
   if [ ${#i} -lt 6 ];then
      data="$data-$i"
   fi
done
echo "Words list length less than 6:$data"
[root@localhost ~]# bash length_base.sh
rabbit length is 6
is length is 2
favorite length is 8
to length is 2
eat length is 3
cabbage length is 7
Words list length less than 6:-is-to-eat

实验2:批量创建用户;

用户名以test开头,按数字序号变化;

一共添加30个账号,即test01,tes02...,test30 用户初始密码为123456

分析: 根据要求使用循环语句完成

创建用户使用useradd命令,根据命名规则,10以下的需要加0

初始账号设置方法(echo “123456” |passwd –stdin username)

[root@localhost ~]# vim user_add.sh
for (i=0;i<=30;i++)
do
   if [ $i -lt 10 ];then
   user=test0$i
   else
   user=test$i
fi
if ! id -u $user &> /dev/null;then  #如果用户不存在(用uid确定用户是否存在)
   useradd $u
   echo "123456" | passwd --stdin $user &> /dev/null
else
   echo "$user is exists..."
fi
done

实验3:将之前用for语句创建的test01-test30用户删除

[root@localhost ~]# vim user_delete.sh
i=1
while [ $i -le 30 ]
do
  if [ $i -lt 10 ];then
  user=test0$i
  else
  user=test$i
  fi
  if id -u $user &> /dev/null;then
  userdel -r $user
  else
  echo "$user is not exists..."
  fi
  let i++
done
root@localhost ~]# bash user_delete.sh
[root@localhost ~]# id test01
id: ‘test01’: no such user

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

[root@localhost ~]# vim sshd.sh
for i in `seq -f "%03g" 1 254`
do
        if [ $i -eq 128 ];then
                continue
        fi
        ping -c 2 -w 1 192.168.80.$i
        if [ $? -eq 0 ];then
                ssh 192.168.80.$i
        fi
done
[root@localhost ~]# bash sshd.sh  #下列IP没有开,所以ping不通
PING 192.168.80.001 (192.168.80.1) 56(84) bytes of data.

--- 192.168.80.001 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

PING 192.168.80.002 (192.168.80.2) 56(84) bytes of data.
64 bytes from 192.168.80.2: icmp_seq=1 ttl=128 time=0.280 ms

--- 192.168.80.002 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.280/0.280/0.280/0.000 ms
PING 192.168.80.003 (192.168.80.3) 56(84) bytes of data.

--- 192.168.80.003 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
方法一:采用exc读取文件,然后进入while循环处理

实验5:while循环读取文件:

方法一:采用exc读取文件,然后进入while循环处理
[root@localhost test5]# cat file
shuju1
shuju2
shuju3
[root@localhost test5]# cat file.sh
#!/bin/bash
exec < file      # <是一个重定向符号,意思是吧右边的文件内容作为标准出入传给exec这个命令
while read a     # read a 从标准 输入读入,然后把值赋给a
do
echo $a
done
[root@localhost test5]# bash file.sh
shuju1
shuju2
shuju3
方法二:使用cat读文件,然后通过管道进入while循环处理
cat file | while read line
do
statement1
done
方法三:通过在while循环结尾,使用输入重定向方式
while read line
do
statement1
done < File

(上图其中exec <  file 中<是一个重定向符号,意思是吧右边的文件内容作为标准出入传给exec这个命令)

(read a 从标准 输入读入,然后把值赋给a)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值