News:

Holy shit, it's 2018 2019 2020 2021 2022 2023 2024, and the US isn't a fascist country! What a time to be alive.

Main Menu

[Bash] Deluser Emulation - How?

Started by LordVader, September 25, 2007, 12:44:28 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LordVader

I am working on a custom script to add/remove users to/from a chroot environment as well as backingup/restoring data (passwd/group files and user folders etc)..
So far so good mostly but im stumped on how to emulate the deluser function, to remove a line(the user instance) from a file(custom passwd file)..

You can see the script here:
http://files.serverhash.com/Debian%7cUbuntu/chroot_ssh/chroot_ssh.txt
And some notes and general info about it here:
http://files.serverhash.com/Debian%7cUbuntu/chroot_ssh/readme.txt

The Deluser function is nearly complete, I just need a way to delete the user data stored from:

deluser=`grep $chrootdir/etc/passwd -e "^$username"`

I am reading up on sed and cat as they seem to be usefull for what I need to do but im unfamiliar with them.
Could someone post an example of using grep similar to what im doing above to pull a line from a file, then show an example of how to delete that line from the file I would appreciate it.

Update:
Got this working basicially maybe a better ways.

touch $chrootdir/etc/passwd.bak
sed -e "/$username/d" < $chrootdir/etc/passwd > $chrootdir/etc/passwd.bak
rm -rf $chrootdir/etc/passwd
cp $chrootdir/etc/passwd.bak $chrootdir/etc/passwd
rm -rf $chrootdir/etc/passwd.bak

Any suggestions appreciated..

iago

#1
I don't know if it's any better, but "grep -v" can be used to match all lines except the one you're looking for. Something like:
cat /etc/passwd | grep -v "^$user:" > /etc/passwd.tmp
mv /etc/passwd.tmp /etc/passwd


LordVader

#2
@iago: I will play around with that thx.

Update: That worked great

cat $chrootdir/etc/passwd | grep -v "^$username:" > $chrootdir/etc/passwd.tmp
mv $chrootdir/etc/passwd.tmp $chrootdir/etc/passwd
rm -rf $chrootdir/etc/passwd.bak

Thx again.