1、扫描 /etc/passwd 文件每一行,如发现 GECOS 字段为空,则将用户名和单位电话为 62985600 填充至 GECOS 字段,并提示该用户的 GECOS 信息修改成功。
#!/bin/bash
while read userinfo
do
user=`echo $userinfo | cut -d: -f1`
memo=`echo $userinfo | cut -d: -f5`
if [[ ! $memo ]]
then
usermod $user -c ${user}62985600 &> /dev/null && echo User ${user}\'s GECOS Successfully modified.
fi
done < /etc/passwd
getent passwd | grep 62985600
[root@centos7 ~]
User abrt's GECOS Successfully modified.
User setroubleshoot's GECOS Successfully modified.
User chrony's GECOS Successfully modified.
User ntp's GECOS Successfully modified.
User gdm's GECOS Successfully modified.
User gnome-initial-setup's GECOS Successfully modified.
User postfix's GECOS Successfully modified.
User tcpdump's GECOS Successfully modified.
User tomcat's GECOS Successfully modified.
User mydql's GECOS Successfully modified.
User mysql's GECOS Successfully modified.
User yinxd's GECOS Successfully modified.
User bash's GECOS Successfully modified.
User testbash's GECOS Successfully modified.
User basher's GECOS Successfully modified.
User sh's GECOS Successfully modified.
User nologin's GECOS Successfully modified.
abrt:x:173:173:abrt62985600:/etc/abrt:/sbin/nologin
setroubleshoot:x:993:990:setroubleshoot62985600:/var/lib/setroubleshoot:/sbin/nologin
chrony:x:992:989:chrony62985600:/var/lib/chrony:/sbin/nologin
ntp:x:38:38:ntp62985600:/etc/ntp:/sbin/nologin
gdm:x:42:42:gdm62985600:/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:988:982:gnome-initial-setup62985600:/run/gnome-initial-setup/:/sbin/nologin
postfix:x:89:89:postfix62985600:/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72:tcpdump62985600:/:/sbin/nologin
tomcat:x:1000:2023:tomcat62985600:/home/tomcat:/bin/bash
mydql:x:1001:2024:mydql62985600:/home/mydql:/bin/bash
mysql:x:1002:2024:mysql62985600:/home/mysql:/bin/bash
yinxd:x:1003:1003:yinxd62985600:/home/yinxd:/bin/bash
bash:x:1004:1004:bash62985600:/home/bash:/sbin/nologin
testbash:x:1005:1005:testbash62985600:/home/testbash:/sbin/nologin
basher:x:1006:1006:basher62985600:/home/basher:/sbin/nologin
sh:x:1007:1007:sh62985600:/home/sh:/sbin/nologin
nologin:x:1008:1008:nologin62985600:/home/nologin:/sbin/nologin