Unix Programming - unexpected results

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2005 > unexpected results





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author unexpected results
Bob

2005-08-26, 2:51 am

Can someone please tell me why this code print the content the current
direcorty as well as the passwords

#!/bin/sh
for password in `awk -F":" '{print $2}' /etc/shadow` ; do
if [ $password != "!!" ] ; then

if [ $password != "*" ] ; then

echo "$password"

fi

fi

done


Chuck Dillon

2005-08-26, 6:00 pm

Bob wrote:
> Can someone please tell me why this code print the content the current
> direcorty as well as the passwords
>
> #!/bin/sh
> for password in `awk -F":" '{print $2}' /etc/shadow` ; do


Because you don't escape the `` command. If it returns any wildcards
it will be expanded to generate the for list. Change the awk call to
echo "*" and see what you get. Try...
for password in "`awk '-F:' '{print $2}' /etc/shadow`" ; do
You should double quote the $password references in your if statements
as well since now at least one of your passwords will contain a wildcard.

-- ced

> if [ $password != "!!" ] ; then
>
> if [ $password != "*" ] ; then
>
> echo "$password"
>
> fi
>
> fi
>
> done
>
>



--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
Barry Margolin

2005-08-26, 6:00 pm

In article <den61g$3sn$1@grandcanyon.binc.net>,
Chuck Dillon <spam@nimblegen.com> wrote:

> Bob wrote:
>
> Because you don't escape the `` command. If it returns any wildcards
> it will be expanded to generate the for list. Change the awk call to
> echo "*" and see what you get. Try...
> for password in "`awk '-F:' '{print $2}' /etc/shadow`" ; do


That won't work, either, because it will treat all the passwords as a
single word, not separate words. Try:

cut -d: -f2 /etc/shadow | while read password
do
...
done

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com