Author Topic: [Bash] Deluser Emulation - How?  (Read 3057 times)

0 Members and 1 Guest are viewing this topic.

Offline LordVader

  • Full Member
  • ***
  • Posts: 113
  • Knowledge is power.
    • View Profile
    • James Moss on the web!
[Bash] Deluser Emulation - How?
« 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..
« Last Edit: September 25, 2007, 02:02:08 am by LordVader »

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Bash] Deluser Emulation - How?
« Reply #1 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

« Last Edit: September 25, 2007, 10:05:22 am by iago »

Offline LordVader

  • Full Member
  • ***
  • Posts: 113
  • Knowledge is power.
    • View Profile
    • James Moss on the web!
Re: [Bash] Deluser Emulation - How?
« Reply #2 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.
« Last Edit: September 26, 2007, 01:17:39 am by LordVader »