Operating System - HP-UX
1832204 Members
2515 Online
110039 Solutions
New Discussion

script formatting problem

 
SOLVED
Go to solution
Khashru
Valued Contributor

script formatting problem

Hi,

I am running the folloing script

echo "enter user name"
read a
echo "Login name User Name"
echo "`cat /etc/passwd |grep $a |cut -d ":" -f1,5 | cut -d "," -f1 |cut -d ":" -f1` `cat /etc/passwd |grep $a |cut -d ":" -f1,5 | cut -d "," -f1 |cut -d ":" -f2`"


It works very good if there is only one user in the password file. the output comes like
Login name User Name
mayub Mohammad Ayub

But when there is more then one person the output comes like

Login name User Name
ssingh
psingh
gsingh
psingh1 Shobhana Singh
Parwin Singh
Gurvinder Singh
Priya Singh

i want then like

Login name User Name
mayub Mohammad Ayub


How can i do that
17 REPLIES 17
Steven E. Protter
Exalted Contributor

Re: script formatting problem

Shalom Khashru,

echo "enter user name"
read a
echo "enter login"
read b
grep $b /etc/passwd | grep $a | awk -F: '{print $1}'
# not sure on the verification part what you want but this will surely get ONLY one line out of /etc/passwd
# note using passwd file this way is not recommended.
# If I've nnot got it exactly right, just add another read statement.

The cut command is inferior to awk in this case because awk can parse this database (that is what passwd is) by its field boundries, the colon.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Khashru
Valued Contributor

Re: script formatting problem

The person who will be using it will not know the login name. he will only know user first name or last name. My plan is to give him the choice so that he can see login name beside user full name and understand what is the login name for that particular user.
Vibhor Kumar Agarwal
Esteemed Contributor

Re: script formatting problem

How about this:

echo "enter user name"
read a
echo "Login name User Name"
ypcat passwd | grep $a | awk -F":" '{print $1,$5}'
Vibhor Kumar Agarwal
Patrick Wallek
Honored Contributor

Re: script formatting problem

How about this instead of your echo statement:

grep $a /etc/passwd | awk -F : '{print $1,$5}'
Khashru
Valued Contributor

Re: script formatting problem

I donot have nis here. so i am using cat. it gives formatted output but there is three,,, back of the name. i need to clear that

Login name User Name
ssingh Shobhana Singh,,,
psingh Parwin Singh,,,
gsingh Gurvinder Singh,,,
psingh1 Priya Singh,,,
Arunvijai_4
Honored Contributor

Re: script formatting problem

Hi,

You can use "awk". It provides better ease use just like, awk -F : '{print $1,$5}'

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Jakes Louw
Trusted Contributor

Re: script formatting problem

Well, if there is no entry in the "description" optional field, you will get results like that. First clean up and correct the /etc/passwd file, then your results will be more consistent, regardless of whether you use cut or awk.

If you want to check for a null (empty) field, then the script gets more complex, but I can help with that.
Trying is the first step to failure - Homer Simpson
Vibhor Kumar Agarwal
Esteemed Contributor

Re: script formatting problem

Are these 3 ,,, as a result of awk or cut.

There might be a error in cutting of the fields.
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: script formatting problem

Can you give as your sample /etc/passwd entry here. It will be helpful.

This script line of,

echo "`cat /etc/passwd |grep $a |cut -d ":" -f1,5 | cut -d "," -f1 |cut -d ":" -f1` `cat /etc/passwd |grep $a |cut -d ":" -f1,5 | cut -d "," -f1 |cut -d ":" -f2`"

is very odd. You have written a simple thing in a big manner. Kwel.

We will help you out. For formatting better use printf rather than echo.

--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: script formatting problem

How you are using more than one persion ? Are you giving input with read a as,

user1 user2?

echo "enter user name"
read a
echo "Login name User Name"
echo "`cat /etc/passwd |grep $a |cut -d ":" -f1,5 | cut -d "," -f1 |cut -d ":" -f1` `cat /etc/passwd |grep $a |cut -d ":" -f1,5 | cut -d "," -f1 |cut -d ":" -f2`"

It will generate for only one user else error. Can you post exact script contents and /etc/passwd entry for two users?

--
Muthu
Easy to suggest when don't know about the problem!
john korterman
Honored Contributor

Re: script formatting problem

Hi,

your grep statement probably matches more than a single name/line. If you run 11.11, try changing:

grep $a
to
grep -w "$a"

which will force grep to match only whole words. This will of course not guarantee match of only a single name/line, but may be an improvement.

regards,
John K.
it would be nice if you always got a second chance
Amit Agarwal_1
Trusted Contributor

Re: script formatting problem

Try out

read a
awk -F':' -v user=$a '{ if(match($1, user)!=0) printf("%s %s\n", $1, $4); }' < /etc/passwd

You can even enhance printf format string as you want.
James R. Ferguson
Acclaimed Contributor

Re: script formatting problem

Hi Khashru:

If you don't mind using perl, the following script will offer what you want.

The script considers the username to be the string of characters in the 'gecos' field up until the first comma if there is one. If the 'gecos' field is empty "[no info]" is reported. If a login name isn't valid, that is reported instead.

# cat ./whatuser
#!/usr/bin/perl
use strict;
use warnings;
my ($name, $gecos);
print "Enter user name (^C to quit)\n";
while (1) {
chomp ($name = <>);
($name,undef,undef,undef,undef,undef,$gecos) = getpwnam $name;
if (defined $name) {
$gecos =~ m/(.+?),/;
printf "%-8s %-16s\n", $name, $1 ? $1 : "[no info]";
} else {
warn "isn't a valid login\n";
}
}

Regards!

...JRF...
Patrick Wallek
Honored Contributor
Solution

Re: script formatting problem

If you are intent on using the shell, then give this a try:

grep $a /etc/passwd | awk -F : '{print $1,$5}' | cut -d , -f 1


Chan 007
Honored Contributor

Re: script formatting problem

Other way,

using finger add $9 if you have users with middle name too.

for i in $(cat /etc/passwd |awk -F ":" '{print $1}')
do
echo $i
finger $i |grep "In real life" |awk '{print $3 ":" $7 $8}'>> /tmp/t1
done
Chan 007
Honored Contributor

Re: script formatting problem

Sorry,

to my reply, you have to cat /vi/view the output at /tmp/t1.

Chan
Khashru
Valued Contributor

Re: script formatting problem

Hi Patrick Wallek,

I user your script and it solved my problem.

Hi JFR,

I donot use parl script.

Thanks