编写 script.sh练习题

编写 script.sh

要求:

1.三个文件

scripts.sh /mnt/userfile /mnt/passwdfile


2.当要建立的用户已经存在时不做任何操作


3.当脚本后所指定文件个数少于2个

please give me userfile or password file


4.当所给文件的行数不一致

/mnt/userfile's line is different /mnt/passwdfile's line


操作思路:

1 输入文件数不少于两个

2.当两文件行数不一致时,报错

3.建立用户

4.设置密码

脚本内容

#!/bin/bash

while [ "$#" -lt "2" ]
do
        echo "Please give me usersfile and password file"
        exit 0
done

NUM1=`wc -l $1 | awk '{print $1}'`
NUM2=`wc -l $2 | awk '{print $1}'`

while [ "$NUM1" -ne "$NUM2" ]
do
        echo "/mnt/$1's line is diferent with /mnt/$2'file's line"
        exit 0
done

for NUM1 in $(seq 1 $NUM1)
do
        USERNAME=`sed -n ${NUM1}p $1`
        PASSWORD=`sed -n ${NUM1}p $2`
        useradd $USERNAME &> /dev/null
    echo $PASSWORD | passwd --stdin $USERNAME > /dev/null
done


执行结果:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值