LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-09-2008, 09:01 AM   #1
flyordie
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Rep: Reputation: 0
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
 
Old 10-09-2008, 09:37 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,956

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by flyordie View Post
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.
 
Old 10-09-2008, 10:17 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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".
 
Old 10-09-2008, 11:00 AM   #4
cyprinidae
Member
 
Registered: Oct 2008
Distribution: Fedora, CentOS, Crunchbang
Posts: 46

Rep: Reputation: 16
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..
 
Old 10-09-2008, 11:06 AM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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
 
Old 10-09-2008, 11:43 AM   #6
Chromezero
Member
 
Registered: Nov 2004
Location: Arizona
Distribution: Slackware, RHEL, others
Posts: 470

Rep: Reputation: 40
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.

Last edited by Chromezero; 10-09-2008 at 11:47 AM.
 
Old 10-09-2008, 12:43 PM   #7
flyordie
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Original Poster
Rep: Reputation: 0
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
thx all

greetz fly
 
Old 10-09-2008, 03:57 PM   #8
cyprinidae
Member
 
Registered: Oct 2008
Distribution: Fedora, CentOS, Crunchbang
Posts: 46

Rep: Reputation: 16
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fedora 8 asking username but during installation i dint give any username ?? herevenku Linux - Newbie 2 08-25-2008 05:37 AM
Constrain user to /home/username directory General Linux - Security 3 04-05-2007 02:52 AM
Give the root Permission to other user for manage File/Directory shipon_97 Linux - Newbie 2 01-28-2007 12:46 PM
Lost username/passwd (Slackware 10.2) PastorFrederick Linux - Newbie 5 12-23-2005 10:15 AM
root passwd and username latika.gk Linux - Newbie 3 05-26-2003 07:24 AM


All times are GMT -5. The time now is 01:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration