Cut fields from /etc/passwd file into variables?

The script must ask the user to enter the user name and check whether the user exists in /etc/passwd (you must allow the partial usernames also). If the username exists, display the details as:

List of users

  Login Name:
  User ID:
  --------------------------------------------------------------

I have started by reading the username:

read username;

Once it pulls my username from the variable I want to cut the first and second field from the /etc/passwd file into their own variables. But this is not working for me and I cannot seem to figure out how I should do this. This is the closest I have came:

grep -E "$username" /etc/passwd|login=$(cut -f1)|userid=$(cut -f2);

Any help would be greatly appreciated!

Regards,

Justin

Seems you've misunderstood the syntax and data flow of the pipeline. Each process in the pipe receives the output from the previous command, munges it a bit, and passes it along to the next process in the pipe. Unfortunately, assignment of the output the way you are attempting isn't possible. All, though, is not lost!

If you read the man page for the cut command you'll notice that you can cut two fields from a file in the same pass. The output from the cut command would be something like scooter:1001 which you can then assign to two variables. The method for assignment depends on the shell you are using. Also, it's easier if you use the --output-delimeter option to have cut remove the colon and replace it with a space.

So, something like this should generate your desired data:

grep "$username" /etc/passwd | cut -f 1,3 -d : --output-delimiter=" "

To assign the output to variables, you can use this command in an array assignment, or assign it to a single variable and split it into two (bash) or assign it straight into two variables if you are using Korn shell.

array=( $( grep "$username" /etc/passwd | cut -f 1,3 -d : --output-delimiter=" " ) )

echo "user=${array[0]}  user-num=${array[1]}"

The previous code will work in both shells. The next code will work only in Korn shell:

grep "$username" /etc/passwd | cut -f 1,3 -d : --output-delimiter=" "  | read user userid

and assigns the values straight to the variables.

You are faced with one problem.... if given a partial user name the output from cut will be more than just two fields. To deal with this, you'll need a bit of a different approach that involves a loop. As this seems more like a homework exercise, than something for work, or maintenance of your personal system, I'll leave you to doing that.

I have found an alternative solution to cutting each field. It works perfectly, but if a partial variable is used it combines each field from all the usernames it finds . I would like to have each username it finds paste each field separately into own heading. I have tried a do while loop but it has no affect. Can anyone help me find an alternative solution or maybe point me in the right direction?
--------------------------------------------------------------------------------------------------

echo Please enter the username to check:;

while read login;

do
    username=$(grep $login /etc/passwd|cut -f1 -d":");
    userid=$(grep $login /etc/passwd|cut -f3 -d":");
    groupid=$(grep $login /etc/passwd|cut -f4 -d":");
    fullname=$(grep $login /etc/passwd|cut -f5 -d":");
    homedir=$(grep $login /etc/passwd|cut -f6 -d":");
    shell=$(grep $login /etc/passwd|cut -f7 -d":");
     
    echo "         List of users";
    echo Login Name    : $username;
    echo User ID       : $userid;
    echo Group ID      : $groupid;
    echo User Full Name: $fullname | tr '[a-z]' '[A-Z]';
    echo Home Directory: $homedir;
    echo Shell         : $shell;
done

Not only is grepping through the file to cut each field very inefficient, it is also the reason you are getting bad results when the user enters a partial name. Because I believe this to be an assignment, I'll only give you some hints to the overall logic:

while exit condition is not met
do
    prompt user
    read search-target
    grep passwd file for search-target |cut all needed fields | while lines read fields
    do
      print fields with headers
    done
done

Another thing that you need to think about is this: If the user enters a user name that is a partial match for something in another field, you might over pick entries that are not desired.