Register
It is currently Fri Oct 31, 2014 9:51 pm

check if directory exists and has contents


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Wed Nov 19, 2008 7:36 pm   

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

Need a bash script to check if a directory called 'assignments' exists in a users home directory
If it doesn't exist, or it is empty, we copy that users name to a file called logfile.
If it does exist then copy the contents to another directory called 'report' in the current directory

Here's what I've done so far....

if [ -d /assignment ]
then >> ./report

else >> ./logfile

fi


Please help.

Thanks,
BB


Top
 Profile  
 PostPosted: Thu Nov 20, 2008 1:57 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Code:
#!/bin/bash
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 ./report
         cp /home/$i/assignment/* ./report/
      fi
   else
      echo "$i" >> ./logfile
   fi
done

Something like this maybe? Havent tried it out since I don't have a linux box around me. But it should work :P
If it doesn't i would recommend that you try it with a nested for-loop instead like this:
Code:
#!/bin/bash
for i in $(ls /home); do
   if [ -d /home/$i/assignment ]; then
      x=0
      for a in $(ls /home/$i/assignment/); do
         [ -d ./report ] || mkdir -p ./report
         cp /home/$i/assignment/$a ./report/
         let x=x+1
      done
      if [ $x -eq 0 ]; then
          echo "$i" >> ./logfile
      fi
   else
      echo "$i" >> ./logfile
   fi
done

This method thou should be alot slower since you're just copying 1 file per itteration.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu Nov 20, 2008 5:06 am   

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
Thank you Fredrik Eriksson. I will give them both a go.


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

Joined: Wed Nov 19, 2008 11:26 am
Posts: 13
The first example gives an error when I try to run it

"line 15: syntax error: unexpected end of file

The second one gives an error message also

"Permission denied"


Thanks


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

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
I'm not entirely sure why it would say unexpected end of file. but you could try running the script with debug and see why.

You'll probably need to run it as root since you're going to muck about in other users directories.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 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