Help With Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help With Script
# 1  
Old 01-14-2010
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
# 2  
Old 01-14-2010
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.
# 3  
Old 01-14-2010
I know that I have to use crone but dont know what commands to use in the script
thanks anyway
# 4  
Old 01-14-2010
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
# 5  
Old 01-14-2010
Thanks

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

Code:
#! /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

Last edited by pludi; 01-15-2010 at 01:57 AM.. Reason: code tags, please...
# 6  
Old 01-15-2010
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
Code:
#! /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

# 7  
Old 01-15-2010
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
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below. #masterscript.sh ./callingscript.sh echo $fileExist #callingscript.sh echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question