change password

change password

am 11.10.2005 17:19:23 von Kris

Hi
I need help for my script that change password placed in other file.

bp= /etc/passwd-
exec < $npl
while read line
do
#for users that exist in /etc/passwd
if grep `echo $line | cut -f 1 -d ':'` $bp
then

#take username from the file
a= echo $line | cut -f 1 -d ':'

#take password from the file
b= echo $line | cut -f 2 -d ':'

echo $b | passwd --stdin $a
fi
done

In this case passwd --stdin doesn't recognize the variable $a.
May you have other ideas for this isue.

Kris

Re: change password

am 11.10.2005 22:40:36 von Bill Marcum

On Tue, 11 Oct 2005 15:19:23 GMT, Kris
wrote:
>
> #take username from the file
> a= echo $line | cut -f 1 -d ':'
>
> #take password from the file
> b= echo $line | cut -f 2 -d ':'
>
> echo $b | passwd --stdin $a
> fi
> done
>
> In this case passwd --stdin doesn't recognize the variable $a.
> May you have other ideas for this isue.
>
The variable $a is empty because you wrote a space after the = sign and
you didn't use ` ` or $( )

--
It's documented in The Book, somewhere...
-- Larry Wall in <10502@jpl-devvax.JPL.NASA.GOV>

Re: change password

am 11.10.2005 23:44:27 von cfajohnson

On 2005-10-11, Kris wrote:
> Hi
> I need help for my script that change password placed in other file.
>
> bp= /etc/passwd-

You have assigned an empty string to bp and then tried to execute
/etc/passwd-. If you ran that, you would have received an error
message.

bp=/etc/passwd

> exec < $npl
> while read line
> do
> #for users that exist in /etc/passwd
> if grep `echo $line | cut -f 1 -d ':'` $bp
> then
>
> #take username from the file
> a= echo $line | cut -f 1 -d ':'

That should be:

a=`echo $line | cut -f 1 -d ':'`

But if you are using a POSIX shell (such as bash or ksh), you
don't need cut, which will slow down your script:

a=${line%%:*}

> #take password from the file
> b= echo $line | cut -f 2 -d ':'

The same applies here, but it's a little more complicated:

temp=${line#"$a":} ## remove the name and first colon
b=${temp%%:*}

> echo $b | passwd --stdin $a
> fi
> done
>
> In this case passwd --stdin doesn't recognize the variable $a.

A faster way would be to store the names from the password file in
a variable and look the names up in that:

set -- `cut -d: -f1 /etc/passwd`
names=" $* "

exec < $npl

## By setting IFS to a colon, read can split the lines for you
while IFS=: read -r a b stuff
do

## Using a here document prevents plaintext passwords from appearing
## in the output of ps
case $names in
*" $a "*) passwd --stdin "$a" <<-. ## Next 2 lines begin with tabs
$b
.
;;
esac
done

--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

Re: change password

am 13.10.2005 18:23:34 von Kris

In article , cfajohnson@gmail.com says...
> On 2005-10-11, Kris wrote:
> > Hi
> > I need help for my script that change password placed in other file.
> >
> > bp= /etc/passwd-
>
> You have assigned an empty string to bp and then tried to execute
> /etc/passwd-. If you ran that, you would have received an error
> message.
>
> bp=/etc/passwd
>
> > exec < $npl
> > while read line
> > do
> > #for users that exist in /etc/passwd
> > if grep `echo $line | cut -f 1 -d ':'` $bp
> > then
> >
> > #take username from the file
> > a= echo $line | cut -f 1 -d ':'
>
> That should be:
>
> a=`echo $line | cut -f 1 -d ':'`
>
> But if you are using a POSIX shell (such as bash or ksh), you
> don't need cut, which will slow down your script:
>
> a=${line%%:*}
>
> > #take password from the file
> > b= echo $line | cut -f 2 -d ':'
>
> The same applies here, but it's a little more complicated:
>
> temp=${line#"$a":} ## remove the name and first colon
> b=${temp%%:*}
>
> > echo $b | passwd --stdin $a
> > fi
> > done
> >
> > In this case passwd --stdin doesn't recognize the variable $a.
>
> A faster way would be to store the names from the password file in
> a variable and look the names up in that:
>
> set -- `cut -d: -f1 /etc/passwd`
> names=" $* "

Thank you Chris, I finished my script based on yours hints.

Kris