Register
It is currently Thu Oct 23, 2014 1:11 pm

Shell script to collect files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 12 posts ] 
Author Message
 PostPosted: Wed Nov 19, 2008 11:36 am   

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
Hi,

I need a shell program that will prompt the user to input a dept name to the script.
The script should then check a specific directory called ‘report’ in the home directory of each member of that dept.
If the report directory does not exist, or there are no contents in the directory, that user's name should be stored in a file called ‘logfile’ in the current directory. The first line in logfile should contain a header ‘List of users with no reports on ‘today’s date and time’ ‘
Otherwise the contents of the directory should be copied to a sub directory called ‘reporting’ in the current directory.
The script should repeat the process until all users in the dept have been checked.

Thanks,
Bobby


Top
 Profile  
 PostPosted: Thu Nov 20, 2008 6:58 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Fairly good description :) , the only question I have is how can it be determined which users/home dirs are in which department?


Top
 Profile  
 PostPosted: Thu Nov 20, 2008 7:23 am   

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
Thanks for your reply :)

#Users have already been assigned to depts when they were added to the system, some will be in accounts, #some in HR etc....

#We ask the person who is running the script to enter a dept name...

echo -e "Please enter the dept name: \c"
read deptname

# then check the /etc/group file for the deptname and usernames

deptno=`cat /etc/group | grep $1 | cut -f3 -d:`

# pass the details to the /etc/passwd file
homeDirectory=`cat /etc/passwd | grep $deptno | cut -f6 -d:`

# in the users home directory we want to check for the existence of a directory called report
# if it's not there, or is empty, we 'echo' the username to the logfile in the current directory
# if it does exist (and has a file in it) we copy that to a directory called reporting in the current directory....

for i in $(ls /home); do
if [ -d /home/$i/assignment ]; then
files=$(ls /home/$i/assignment/)
if [ -z $files ]; then
echo "$i" >> ./logfile
else
[ -d ./report ] || mkdir -p ./reporting
cp /home/$i/assignment/* ./reporting/
fi
else
echo "$i" >> ./logfile
fi

## not sure how to tie all these elements together into one working script :?

Thanks again :)


Top
 Profile  
 PostPosted: Thu Nov 20, 2008 8:37 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Code:
# Written by: Fredrik Eriksson, 2008
echo -e "Please enter the dept name: \c"
read deptname

# Get all the usernames from /etc/group and replace any spaces trailing a comma.
deptusers=$(grep $deptname /etc/group | cut -d: -f3 | replace ", " ",")
# Set the itteration seperator from whitespace to comma
IFS=","
# Start itterating all the usernames
for i in $deptusers; do
   # Check if the user's assignment directory exists for this itteration
   if [ -d /home/$i/assignment ]; then
      # If it was found, create a variable with the content.
      files=$(ls /home/$i/assignment/)
      # If we don't find any files the $files variable should be empty.
      if [ -z $files ]; then
         # If it was, print the username to ./logfile
         echo "$i" >> ./logfile
      else
         # If it's not empty, check if the ./report exists otherwise create it.
         [ -d ./reporting ] || mkdir -p ./reporting
         # Copy the content of assignment directory to ./reporting
         cp /home/$i/assignment/* ./reporting/
      fi
   else
      echo "$i" >> ./logfile
   fi
done


Top
 Profile  
 PostPosted: Fri Nov 21, 2008 2:00 pm   

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
Thank you Fredrik. I will give this a go. Much appreciated


Top
 Profile  
 PostPosted: Mon Dec 08, 2008 8:40 am   

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
what is happening in this line?

if [ -z $files ]; then

not sure what -z does?

Thanks


Top
 Profile  
 PostPosted: Mon Dec 08, 2008 8:44 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
[ -z $files ] checks if the variable $files is empty or not.

to be more specifik it checks if the [ ] is empty or not.

[ -z $files ] would be translated (for example) to [ -z /home/$i/assignment/file ] or if it didn't find anything [ -z ].

So -z says that if there no trailing characters then the statement is true, if there is the statement is false.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Dec 08, 2008 8:47 am   

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
Thanks again :)


Top
 Profile  
 PostPosted: Wed Dec 10, 2008 8:43 pm   

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
I'm getting an error message - "too many arguments" - when I run this section of code. It's saying there is a problem with the line highlighted in red??

# read the variables in each line of the temp file
while read var1 var2 var3 var4 var5 var6 <&3
do

# check if report directory exists for each user
if [ -d /home/user/$var4/$var5/$var6/report ]; then

# Create a variable with the content if directory is found
files=$(ls /home/user/$var4/$var5/$var6/report/*)

# if no files exist in the dir, the $file variable should be empty
if [ -z $files ]; then


echo "$v6ar" >> ./logfile
else

cp /home/user/$var4/$var5/$var6/report/* ~/reporting
fi
else

echo "$v6" >> ./logfile
fi
done

Is there anyway to resolve this?

Thanks
Bobby


Top
 Profile  
 PostPosted: Wed Dec 10, 2008 8:49 pm   

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
p.s. the script is doing what it's supposed to do, it's just that I'd prefer if I could get rid of the this (too many arguments) error when I run it.

Thx


Top
 Profile  
 PostPosted: Thu Dec 11, 2008 2:05 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
try encasing $files with quotation marks, "" (not entirely sure that'll work thou, but usually when it says too many arguments it's because of whitespace)
Also if that doesn't work you can try doing it with them and reversing the order. Add a ! -z $files and do the cp first then "else" echo

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Sat Dec 13, 2008 4:18 pm   

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
Quotation marks solved the problem.

Many thanks :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP