2

new to bash scripting so just wondering if i am doing this code right at all. im trying to search /etc/passwd and then grep and print users.

usage ()
{
     echo      "usage: ./file.sk user"
}
# test if we have two arguments on the command line
if [ $# != 1 ]
then
    usage
    exit
fi

if [[ $# < 0 ]];then
   usage
   exit
fi

# Search for user
fullname=`grep $1 /etc/passwd | cut -f 5 -d :`
firstname=`grep $1 /etc/passwd | cut -f 5 -d : | cut -f 1 -d " "`

#check if there. if name is founf: print msg and line entry
not sure as how to this or if im doing this right...

am i doing this right?

3
  • Your second $# test is redundant, if the code reaches it $# must be 1. Commented Jan 16, 2012 at 19:06
  • And have a look at the finger command. Commented Jan 16, 2012 at 19:10
  • Unquoted < is redirection, not numeric comparison. As others have pointed out, the condition is redundant anyway, but this error could be pretty hard to debug, so I'm pointing it out separately. Commented Jan 16, 2012 at 19:59

5 Answers 5

3
grep $1 /etc/passwd | while IFS=: read -r username passwd uid gid info home shell
do
  echo $username: $info
done
Sign up to request clarification or add additional context in comments.

Comments

2

This might work for you:

fullname=$(awk -F: '/'$1'/{print $5}' /etc/passwd)
firstname=${fullname/ *}

Comments

1

You're on the right track.

But I think the 2nd if [[ $# < 0 ]] .... fi block doesn't get you much. Your first test case gets the situation right, 'This script requires 1 argument or quits'.

Also, I don't see what you need firstname for, so a basic test is

 case "${fullname:--1}" in 
   -[1] ) printf "No userID found for input=$1\n" ; exit 1 ;;
   * )
     # assume it is OK
     # do what every you want after this case block
   ;;
 esac

You can of course, duplicate this using "${firstname}" if you really need the check.

OR as an equivalent if ... fi is

 if [[ "${fullname}" == "" ]] ; then
    printf "No userID found for input=$1\n" ; exit 1
 fi

note to be more efficient, you can parse ${fullname} to get firstname without all the calls to grep etc, i.e.

 firstname=${fullname%% *}

Let me know if you need for me to explain :--1} and %% *} variable modifiers.

I hope this helps.

Comments

1

Instead of this:

fullname=`grep $1 /etc/passwd | cut -f 5 -d :`
firstname=`grep $1 /etc/passwd | cut -f 5 -d : | cut -f 1 -d " "`

Try this:

fullname=$(cut -f5 -d: /etc/passwd | grep "$1")

if [[ $? -ne 0 ]]; then
    # not found, do something
fi

firstname=${fullname%% *} # remove the space and everything after

Note that I changed my answer to cut before grep so that it doesn't get false positives if some other field matches the full name you are searching for.

1 Comment

That's OK, I'll update my answer again, but in the meantime I made some improvements, so thanks anyway!
0

You can simply by reading your input to an array and then printing out your desired fields, something like this -

grep $1 /etc/passwd | while IFS=: read -a arry; do 
    echo ${arry[0]}:${arry[4]}; 
done

Test:

jaypal:~/Temp] echo "root:*:0:0:System Administrator:/var/root:/bin/sh" | 
while IFS=: read -a arry; do 
    echo ${arry[0]}:${arry[4]}; 
done
root:System Administrator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.