LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   i need to find the username in field5 of a user i give in the directory /etc/passwd (https://www.linuxquestions.org/questions/linux-newbie-8/i-need-to-find-the-username-in-field5-of-a-user-i-give-in-the-directory-etc-passwd-675302/)

flyordie 10-09-2008 08:01 AM

i need to find the username in field5 of a user i give in the directory /etc/passwd
 
i need to write a script (like vi script1.sh)
so when i type for example:" bash script1.sh user1 "
the script needs to go into /etc/passwd to check if the user excists and then say: user1 excists and his real neam is "whatever stands in the info field behind that user (field 5)"
does anybody has any idea?

greetz fly

TB0ne 10-09-2008 08:37 AM

Quote:

Originally Posted by flyordie (Post 3305158)
i need to write a script (like vi script1.sh)
so when i type for example:" bash script1.sh user1 "
the script needs to go into /etc/passwd to check if the user excists and then say: user1 excists and his real neam is "whatever stands in the info field behind that user (field 5)"
does anybody has any idea?

greetz fly

Yep, lots of them. My first idea is that this sounds like homework.

Post what you've tried already, and we can help you.

jschiwal 10-09-2008 09:17 AM

The getent program can look up a user. The id program can give you a users uid. These aren't scripts, and are not what your teacher will be looking for if this is a homework assignment. You might want to read "info sed" or "man grep".

cyprinidae 10-09-2008 10:00 AM

To actually answer your question... it can be something like this:

Code:

#!/bin/bash
if [ -z $1 ]; then
 echo "Please specify the user name";
else
 name=`cat /etc/passwd |grep $1`;
 if [ $? -eq 0 ]; then
  echo `echo $name | awk -F: {' print$5 '}`;
 else
  echo "User doesn't exist";
 fi
fi

It doesn't check if the 5th field is empty and it can be definitely more elegant and shorter, but nah..

PTrenholme 10-09-2008 10:06 AM

Here's a script for you. Remember, though, that if it's a homework problem, you'll need to be able to explain it to your instructor:
Code:

#!/bin/bash
getent passwd | cut -d: -f1,5 | cut -d, -f1 | sed s/.*:$// | grep -v ^[[:space:]]*$ | sed s/:/\\\t/ | grep $1


Chromezero 10-09-2008 10:43 AM

You could use "finger" and save a few steps...
Code:

finger - user information lookup program
Synopsis

finger [-lmsp] [user ...] [user@host ...]

    Description

The finger displays information about the system users.


flyordie 10-09-2008 11:43 AM

i tried it this way

Code:

user=`grep $1 /etc/passwd | cut –f5 –d”:”`
if test `grep $1 /etc/passwd | cut –f5 –d”:”`
then
echo $1 “ is from “ $user
else
echo $1 “ doesn't exists”
fi

but then when field 5 is empty, it says: user doesn't excists...
i'll try some of your answers, thx
and yes, it's homework, when i'll take something, i'll make sure i know how to explain it, i'm stupid, but not that stupid :p
thx all

greetz fly

cyprinidae 10-09-2008 02:57 PM

Flyordie,
that's true. It goes straight to 'doesn't exist' if there is no user or the the 5th field is empty. You will have to add separate statement for checking what 'grep' returns and other to check what 'cut' returns when 'grep' was successful.


All times are GMT -5. The time now is 03:40 AM.