[K12OSN] OT Help with file deletion script

James P. Kinney III jkinney at localnetsolutions.com
Thu Oct 25 16:24:03 UTC 2007


On Thu, 2007-10-25 at 10:55 -0500, Jim Kronebusch wrote:
> > Remove the "do" from line 10 and the "done" from line 13. The
> > if ...
> > then
> > .
> > .
> > .
> > else
> > .
> > .
> > fi
> > 
> > is sufficient grouping.
> 
> Funny, I did that already :-)  Here is what I have now:
> 
> #!/bin/sh
> cd /home
> for userdir in `ls -C1`
> do
>   echo Working on $userdir...
>   cd /home/$userdir
>   for deleteme in `ls -C1 -a`
>   do
>    if [ $deleteme != mail ] || [ $deleteme != .openwebmail ]
>     then
>       echo Removing $deleteme...
>       rm -ri $deleteme
>     else echo "Oops, encountered "$deleteme", not deleting!"
>     fi
>   done
> done
> 
> I pulled the extra [ and ] from line 8 as well.  Otherwise it gave me errors like so all
> the way through and didn't remove anything:
> 
> ./homedir_purge: 16: [[: not found
> ./homedir_purge: 16: [[: not found
> Oops, encountered .update-manager-core, not deleting!
> ./homedir_purge: 16: [[: not found
> ./homedir_purge: 16: [[: not found
> Oops, encountered .update-notifier, not deleting!
> ./homedir_purge: 16: [[: not found
> ./homedir_purge: 16: [[: not found
> Oops, encountered Videos, not deleting!
> ./homedir_purge: 16: [[: not found
> ./homedir_purge: 16: [[: not found
> Oops, encountered .Xauthority, not deleting!
> 
> But now it removes everything and doesn't pay attention to the 2 exceptions.
> 
> I am reading some script tutorials right now.  Other suggestiosn?

Put " around the $deleteme and the value being checked against in the if
line. This will force character mode. Change the OR to an AND in the if
line ||->&&  otherwise it will always return true and delete everything.

I like :

#!/bin/sh
cd /home
for userdir in `ls -C1`
do
  echo Working on $userdir...
  cd /home/$userdir
  for deleteme in `ls -C1 -a`
  do
   if [ "$deleteme" == "mail" ] || [ "$deleteme" == ".openwebmail" ]
    then
      echo "Oops, encountered "$deleteme", not deleting!"
    else
      echo "Removing $deleteme..."
      rm -ri $deleteme
   fi
  done
done
> 
> -- 
> This message has been scanned for viruses and
> dangerous content by the Cotter Technology 
> Department, and is believed to be clean.
> 
> _______________________________________________
> K12OSN mailing list
> K12OSN at redhat.com
> https://www.redhat.com/mailman/listinfo/k12osn
> For more info see <http://www.k12os.org>
> 
-- 
James P. Kinney III          
CEO & Director of Engineering 
Local Net Solutions,LLC        
770-493-8244                    
http://www.localnetsolutions.com

GPG ID: 829C6CA7 James P. Kinney III (M.S. Physics)
<jkinney at localnetsolutions.com>
Fingerprint = 3C9E 6366 54FC A3FE BA4D 0659 6190 ADC3 829C 6CA7
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://listman.redhat.com/archives/k12osn/attachments/20071025/8c8e54e4/attachment.sig>


More information about the K12OSN mailing list