(+84) 236.3827111 ex. 402

Linux Shell: Viết chương trình shell tạo user theo thông tin trong file user.db


Viết chương trình shell tạo user theo thông tin trong file user.db

Cho file user.db như sau:

#cot1-username:cot2-NgaySinh(dd/mm/yyyy)

sv1:12/12/2020

sv2:20/09/1999

sv30:03/01/2001

Tạo user theo cột 1 - nhớ kiểm tra user đã tồn tại chưa.

Mật khẩu của user là NgaySinh theo định dạng ddmmyyyy.

Tất cả user được tạo đều thuộc nhóm CS226.


[root@cntt ~]# cat user1.db

u1000:20/09/2000

u2000:15/06/2003

u3000:25/07/2004

[root@cntt ~]# cat create.user

#Chuong trinh tao user theo danh sach trong file user1.db

{

while read dong

do

u=`echo $dong | cut -d':' -f1`

p=`echo $dong | cut -d':' -f2`

useradd $u -g CS226

echo $p | passwd $u --stdin

done

}<$1

[root@cntt ~]# chmod u+x create.user

[root@cntt ~]# ./create.user user1.db

Changing password for user u1000.

passwd: all authentication tokens updated successfully.

Changing password for user u2000.

passwd: all authentication tokens updated successfully.

Changing password for user u3000.

passwd: all authentication tokens updated successfully.

[root@cntt ~]#

//Chương trình này chưa kiểm tra sự tồn tại của user trên hệ thống trước khi tạo. Các bạn có thể viết tiếp! :)