Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to remove home diretory 1

Status
Not open for further replies.

lpblauen

Technical User
Dec 2, 2004
193
US
I'm having a problem getting this to work. I have a file called bull_dog.list. In it is the following
test1
test2 keep
test3
test4

I want the script to cat the list and if it finds the word keep not to remove the directory. Other wise remove it all.

cat bull_dog.list | while read H
do
echo " Removing $H Id and home directory"
if `grep "$H keep"`
then
print "keep home directory"
else
print "remove home directory"
fi
done

Ideas??
 
for i in `cat bull_dog.list`
do
if [ $i = "WORD" ];then
echo "Not removing $i"
else
rmdir $i
fi
done

Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
Hi

Code:
while read H K; do
  echo "Home : $H ; Keep $K"
  if [ "$K" == "keep" ]; then          
    print "keep home directory"
  else
    print "remove home directory"
  fi
done < bull_dog.list

Feherke.
 
or


awk '{print "rmdir "$1}' bull_dog.list | grep -v keep > remove.list

Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
I tried both scripts still not working. Here is the new script.
for H in `cat bull_dog.list`
do
echo " Removing $H Id and home directory"
if [ "$H" = "keep" ]
then
print "keep home directory"
else
print "remove home directory"
fi
done
Here is the results

Removing test5 Id and home directory
remove home directory
Removing test6 Id and home directory
remove home directory
Removing keep Id and home directory
keep home directory
Removing test1 Id and home directory
remove home directory

the bull_dog.list is this.
test5
test6 keep
test1
 
What the end result should be is I get a list form somewhere I run the script and it will use 1 of 2 commands. If there is a word keep then we will run userdel userid.
otherwise we run userdel -r userid. The first one only removes there id from the passwd file but leaves there home directory. If there is no keep we remove there user id from passwd and there home directory. Should be simple but I stuck.
 
Hi

I think the second column tells if the directory mentioned in the first column should be kept or not.

Mike, in your [tt]awk[/tt] $2 is not checked. Where you [tt]grep[/tt] for "keep", there is no such value anymore.
Code:
awk '$2!="keep"{print "rmdir " $1}' bull_dog.list | sh
By the way, that was UUOG. ;-)

Feherke.
 
Hi

lpblauen, you are reading in a whole line then compare it with "keep", which should be only one word in the line.

My sample works :
Code:
[blue]master # [/blue]echo "test1
test2 keep
test3
test4" | \
while read H K; do
echo "Home : $H ; Keep : $K"
if [ "$K" != "keep" ]; then echo "remove it"; else echo "keep it"; fi; done
Home : test1 ; Keep :
remove it
Home : test2 ; Keep : keep
keep it
Home : test3 ; Keep :
remove it
Home : test4 ; Keep :
remove it
So again my code, adapted to your exact requirements :
Code:
while read H K; do
  userdel -r "$H"
  test "$K" != "keep" && rm -rf "/home/$H"
done < bull_dog.list

Feherke.
 
Feherke said:
By the way, that was UUOG

I had to stop myself from using it twice.



Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
I guess I'm lost. I need to cat the file??

cat bull_dog.list | while read H K
do
userdel "$H"
test "$K" !="keep" && rm -rf "/home/$H"

This would mean all users would have there home in /home
we have some that have there home directory in /cyborg or /usr/users.

I want to use the userdel command. If I use it without the
-r it only removes there id from passwd file. If I use the -r it would read the passwd file and find there home directory and remove it too.
 
One way:
while read H K
do
[ -z "$K" ] && r=r || r=
userdel $r "$H"
done < bull_dog.list

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

lpblauen said:
I want to use the userdel command. If I use it without the -r it only removes there id from passwd file. If I use the -r it would read the passwd file and find there home directory and remove it too.
Oops. I did not know that. I am not too good in administration. Then listen too PVH, his solution is what you need.

Feherke.
 
I guess today is not my day. I'm still not clear on this part.

while read H K

do I put
cat bull_dog.list | while read H K
or how do I get the H K
 
Hi

Is not clear or is not working ?
Code:
while read H K              [gray]# the input will be splited on IFS into the give two variables[/gray]
do
  [ -z "$K" ] && r=r || r=  [gray]# if $K is empty then variable r's value will be "r" otherwise empty[/gray]
  userdel $r "$H"           [gray]# delete the user passing the value of $r as parameter[/gray]
done < bull_dog.list        [gray]# the while will run as child process getting its input from the given file[/gray]

Feherke.
 
OOps, sorry for the typo:
[ -z "$K" ] && r=[!]-[/!]r || r=

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the help I got it working over lunch. I guess I needed some brain food. Anyway here is the script and it works great.
#!/usr/bin/ksh
#set -x
rm bd.log

cat bull_dog.list | while read H K
do
{
echo "Home : $H ; Keep $K"
if [ "$K" = "keep" ]; then
print "keeping $H home directory"
userdel $H
else
print "removeing $H home directory"
userdel -r $H
fi
} >>bd.log
done < bull_dog.list
pg bd.log
 
You while loop should really have TWO times bull_dog.list as input ?
My simplest way, so far:
while read H K
do userdel ${K:+-r} $H
done < bull_dog.list >> bd.log

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
One liner?
Code:
sed 's/^/-r /g;/keep/s/^-r //;s/keep/;/g' bull_dog.list | xargs  -l userdel
[bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top