Mail Archives: cygwin/2003/05/12/14:08:47
On Mon, 12 May 2003, Bernard Kash wrote:
> "Igor Pechtchanski" <pechtcha AT cs DOT nyu DOT edu> wrote in message
> news:<Pine DOT GSO DOT 4 DOT 44 DOT 0305121055540 DOT 393-100000 AT slinky DOT cs DOT nyu DOT edu>...
> > On Mon, 12 May 2003 Sylvain DOT Ferriol AT imag DOT fr wrote:
> >
> > > Surlignage Max Bowsher <maxb AT ukf DOT net>:
> > >
> > > > Sylvain DOT Ferriol AT imag DOT fr wrote:
> > > > > Yes , because i don't want to create a shell doing this: grep -v
> > > > > $USERNAME /etc/passwd > /etc/passwd
> > > >
> > > sorry
> > > but i don't know with sed how to delete a line starting with $USER
> >
> > For the record: "sed '/'$USER':/d". However, grep is perfectly
> > adequate. What Max was warning you about was the shell redirection
> > mechanism. You'd be safer using something like
> >
> > cp -p /etc/passwd /etc/passwd-bak-tmp && grep -v $USERNAME > /etc/passwd-bak-tmp > /etc/passwd && rm -f /etc/passwd-bak-tmp
>
> Unfortunately, if you have a user named Homer that uses
> 'home' as his userid, the grep will eliminate more users
> than you might expect. A more realistic example might be
> two users 'ted' and 'ed' when modifying user 'ed'.
Umm, yes, you're quite right. I was addressing the redirection comment,
but should have noticed this as well. Ok, here we go. You can use either
of the below:
(1) grep -v "^$USER:"
(2) sed '/^'"$USER"':/d'
(3) sed 's/^'"$USER"':.*$//'
(4) awk -F: '$1!="'"$USER"'"{print}'
(5) perl -pe 's/^'"$USER"':.*$//'
...
Well, you get the drift... (1), (3), (4), and (5) can be used in a
case-insensitive manner (how is left as an exercise for the reader). ;-)
Igor
> Also, the mkuser -p option expects a base directory,
> so if $HOME is "/home/usrname" the resulting record
> will set HOME to "/home/username/username"
>
> I've used awk delete a user from passwd...
>
> awk -F: -v usr=$USERNAME '{IGNORECASE=1; if($1 != usr) print $1;}'
> /etc/passwd
>
> BB
>
> > > > Care! That will leave you with an empty /etc/passwd.
> > > > The shell truncates /etc/passwd BEFORE grep reads it.
> > > >
> > > > > mkpasswd -l -u $USERNAME -p $HOME >> /etc/passwd
--
http://cs.nyu.edu/~pechtcha/
|\ _,,,---,,_ pechtcha AT cs DOT nyu DOT edu
ZZZzz /,`.-'`' -. ;-;;,_ igor AT watson DOT ibm DOT com
|,4- ) )-,_. ,\ ( `'-' Igor Pechtchanski
'---''(_/--' `-'\_) fL a.k.a JaguaR-R-R-r-r-r-.-.-. Meow!
Knowledge is an unending adventure at the edge of uncertainty.
-- Leto II
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -