Clan x86

Technical (Development, Security, etc.) => Unix / Linux Discussion => Topic started by: LordVader on September 25, 2007, 12:44:28 am

Title: [Bash] Deluser Emulation - How?
Post by: LordVader on September 25, 2007, 12:44:28 am
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:
Code: [Select]
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.
Code: [Select]
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..
Title: Re: [Bash] Deluser Emulation - How?
Post by: iago on September 25, 2007, 10:03:13 am
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

Title: Re: [Bash] Deluser Emulation - How?
Post by: LordVader on September 26, 2007, 12:01:10 am
@iago: I will play around with that thx.

Update: That worked great
Code: [Select]
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.