[CentOS] OT: bash script for passwd and shadow manipulation

Erick Perez eaperezh at gmail.com
Thu Nov 27 21:04:01 UTC 2008


On Thu, Nov 27, 2008 at 2:37 PM, Stephen Harris <lists at spuddy.org> wrote:
> On Thu, Nov 27, 2008 at 01:56:11PM -0500, Erick Perez wrote:
>> So far, cut returns the name up to the space, so in this case it will
>> return Kimaura and not Kimaura Thomas.
>
> No, it doesn't.
>
>> # for linia in `cat /etc/passwd`
>
> This is your mistake.  Think about it
>  for linia in `cat /etc/passwd`
>  do
>    echo Line just read: $linia
>  done
>
> That shows what is going wrong; the "in" is splitting at the white space.
>
> What you should be doing
>  cat /etc/passwd | while read linia
> instead.
>
> Even better would be
>  while read linia
>  do
>   ....
>  done < /etc/passwd
>
> Or, to rewrite the whole program in a one line awk script:
>
> awk -F: '{printf("zmprov ma %s@'$domain' displayName %s\n",$1,$5)}' /etc/passwd > $file
>
> --
>
> rgds
> Stephen
> _______________________________________________
> CentOS mailing list
> CentOS at centos.org
> http://lists.centos.org/mailman/listinfo/centos
>

Stephen, you are right. i learned a bit more about "for" cycles and
the blank space was being stripped.
Rainer, some backquotes as you said were missing.

So the code ends like this, and it works :))

#while read linia
#do
#    user=`echo $linia | awk -F :  '{ print $1 }'`
#    nombre=`echo $linia | awk -F :  '{ print $5 }'`
#    echo "el nombre es $nombre"
#    echo "zmprov ma $user@$domain displayName $nombre">>$file
#
#    x=$[x+1]
#done < /etc/passwd

Stephen, your single line awk command is wonderful.
the while cycle takes about a minute (it is a very very long passwd
file) the awk took less than 10 seconds

.........I have so much to learn....
-- 
------------------------------------------------------------
Erick Perez


More information about the CentOS mailing list