0

I would need to create a script that daily checks if users have a home folder home/testuser and if not, then create one. The issue is that I would like to exclude built-in accounts from this example: Administrator, guest, samba and others. The problem occurs when users are created using aduc and not with zentyal.

I would use cron for the daily check, but lacking the knowledge how to exclude the built-in users.

Thanks in advance!

1 Answers1

0

As has been said, we don't normally answer programming questions here, but this sounded like a fun five minute exercise.

This is offered to you without any indication that it works as you expect because my system always creates the users home, and no, I am not about to start playing around and messing up the passwd file to test this for you. So, run this in a text box first, or comment out the bits that do anything (mkdir and chown) and put in some echo commands.

All normal caveats apply: no support is offered or implied; the cheques in the post; and my favourite - its fix in the next release.

#!/bin/bash
# Check if users home directory exists

while read -r LINE; do userID=echo $LINE | awk -F: '{print $3}' if [ "$userID" -ge "1000" -a "$userID" -lt "65534" ] ; then userName=echo $LINE | awk -F: '{print $1}' userHome=echo $LINE | awk -F: '{print $6}' userGroup=echo $LINE | awk -F: '{print $4}'

if [ ! -d "$userHome" ]; then 
  echo found user $userID $userName is missing home:  $userHome
  mkdir $userHome 
  chown $userName:$userGroup $userHome 
fi

fi done < /etc/passwd

Check if users home directory exists

while read -r LINE; do userID=echo $LINE | awk -F: '{print $3}' if [ "$userID" -ge "1000" -a "$userID" -lt "65534" ] ; then userName=echo $LINE | awk -F: '{print $1}' userHome=echo $LINE | awk -F: '{print $6}' userGroup=echo $LINE | awk -F: '{print $4}'

if [ ! -d &quot;$userHome&quot; ]; then 
  echo found user $userID $userName is missing home:  $userHome
  mkdir $userHome 
  chown $userName:$userGroup $userHome 
fi

fi done < /etc/passwd

Do be careful - you can do a lot of damage to your system running any sort of file as root (which this will need). TEST TEST AND TEST AGAIN.