Help With Script

I need a bash script that every day at 4 am verifys the home user directory for the next entrys

  • Registers all user action in root/login/checl.log
  • Excluds UID inferior then 500 and that home/dev/null
  • Create's the the user home directory if it does not exist
  • guarantees that the the home directory belongs to the right users
  • removes all premissions for users other
  • if the files /WWW/index.html does not existe create it

if anybody can help thanks

While we try and help as much as possible, we won't do your work for you. Show what you've got so far & where you're stuck, and we'll do our best.

I know that I have to use crone but dont know what commands to use in the script
thanks anyway

Well then, here are some commands you might want to take a look at:

  • For filtering out the users: awk
  • Creating the home directory: mkdir -p
  • Correcting ownership: chown
  • Removing permissions for everyone else: chmod
  • Testing for file existence: test -e
  • Creating a file: echo or cat or cp it from somewhere
  • Doing that for all users in a file: while read

Thanks

---------- Post updated 15-01-10 at 12:08 AM ---------- Previous update was 14-01-10 at 04:54 PM ----------

#! /bin/bash

infoUsers=$(cat /etc/passwd | cut -d':' -f1,3,6) 
for infoUser in $infoUsers
do 
	userName=$(echo $infoUser | cut -d':' -f1)
	uid=$(echo $infoUser | cut -d':' -f2)
	uDir=$(echo $infoUser | cut -d':' -f3)
	idGroup=$(cat /etc/passwd | cut -d':' -f4)
	if [ '"$uid"  -ge "500" && ! $uDir -eq "/dev/null"' ];
	then
		if [ ! -d $uDir ]
		then
			$(mkdir $uDir)
		fi
		$(chown $userName $uDir)
		groupName=$(cat /etc/group | grep $idGroup | cut -d':' -f1)
		$(chgrp $groupName $uDir)
		$(chmod o-rwx $uDir)
		if [ ! -f "$uDir/www/index.html" ]
		then
			$(echo "Welcome!" > "$uDir""/www/index.html")
			$(chmod o-w+rx "$uDir""/www/index.html")
		fi
	fi
done

for file in /root/logins/login*
do
	echo "$file" >> "/root/logins/check.log"
	cat "$file" >> "/root/logins/check.log"
done

well this is what a got, but when I was going to test it this script bust up my users or something and my linux got **** up. What you think was the problem

What happened to your system? Can you describe any effects, or post any output?

Where corrections were applied:

  • Old code is in red
  • New code is in Green
  • Comments are in Blue
#! /bin/bash

infoUsers=$(cat /etc/passwd | cut -d':' -f1,3,6)
# If you want the group, too, then you'll have to get it here, not inside the loop
infoUsers=$(cat /etc/passwd | cut -d':' -f1,3,6,4)

for infoUser in $infoUsers
do 
	userName=$(echo $infoUser | cut -d':' -f1)
	uid=$(echo $infoUser | cut -d':' -f2)
	uDir=$(echo $infoUser | cut -d':' -f3)

	idGroup=$(cat /etc/passwd | cut -d':' -f4)
# See above comment. Better to get the group outside the loop, not inside
	idGroup=$(echo $infoUser | cut -d':' -f4)

	if [ '"$uid"  -ge "500" && ! $uDir -eq "/dev/null"' ];
# As soon as you surround a variable or constant with quotes it becomes a
string, which isn't suitable for testing with -gt/-lt
# Also, you don't have to quote the whole test expression
	if [ $uid -ge 500 && ! "$uDir" -eq "/dev/null" ];

	then
		if [ ! -d $uDir ]
		then

			$(mkdir $uDir)
# No need to capture the output of mkdir here using $()
			mkdir $uDir

		fi

		$(chown $userName $uDir)
#Again, no need to capture
		chown $userName $uDir

# This is optional. You can change the ownerships by ID just as well
		groupName=$(cat /etc/group | grep $idGroup | cut -d':' -f1)

		$(chgrp $groupName $uDir)
		$(chmod o-rwx $uDir)
#Again, no need to capture
		chgrp $groupName $uDir
		chmod o-rwx $uDir

		if [ ! -f "$uDir/www/index.html" ]
		then
			$(echo "Welcome!" > "$uDir""/www/index.html")
			$(chmod o-w+rx "$uDir""/www/index.html")
		fi
	fi
done

for file in /root/logins/login*
do
	echo "$file" >> "/root/logins/check.log"
	cat "$file" >> "/root/logins/check.log"
done

well my sistem got all the test replaced by xxxxxx and [][][][][][][] then I restarted it but it will not boot now so I installed a new one, i'm running this on a virtual machine. Thanks for every thing

well I cant get this to work, it never enters the first if because I need it to get the UserId line by line and not all at once.
Can anyboady help ???

---------- Post updated at 07:18 PM ---------- Previous update was at 07:08 PM ----------

#!/bin/bash


userinfo=`cat /etc/passwd | cut -d':' -f1,3,4,6`

for user in userinfo 
do
	UserN=`echo "$userinfo" | cut -d':' -f1`
	UserId=`echo "$userinfo" | cut -d':' -f2`
	UserDir=`echo "$userinfo" | cut -d':' -f4`
	UserG=`echo "$userinfo" | cut -d':' -f3`

	if [ $UserId -ge 501 && ! "$UserDir" -eq "/dev/null" ];then    
		
	    if [ ! -d "$UserDir" ];then
	    mkdir $UserDir
	    echo "---Directorio do $UserN criado com sucesso---"
	    fi
	    #chown $UserN $UserDir
	    
            #GroupN=`cat /etc/group | grep $UserG | cut -d':' -f1`
	    #chgrp $GroupN $UserDir
            #chmod o-rwx $UserDir
	    
	    #if [ ! -f "$UserDir/www/index.html" ];then
	    #    $(echo "Hello está no ficheiro Index.html" > "$UserDir""/www/index.html")
	    #	$(chmod o-w+rx "$UserDir""/www/index.html")
	    #fi
	fi
done

Replace this:

if [ $UserId -ge 501 && ! "$UserDir" -eq "/dev/null" ];then

with:

if [ $UserId -ge 501 ] && [ "$UserDir" != "/dev/null" ];then