Display usernames and their UIDs

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

Write a script that displays all usernames and their UIDs in the following fashion:
name1 uid=999
name2 uid=888
... ...

  1. Relevant commands, code, scripts, algorithms:

grep, cut, id, echo, for...do...done

  1. The attempts at a solution (include all code and scripts):

I managed to list usernames and UIDs but I can't figure out how to add "uid=" string. I've been coming back to this problem for the last two days but maybe my approach is wrong. Please, help :frowning:
Since I don't have a Solaris or Linux on PC, I've tested the code on tutorialspoint/execute_ksh_online.

#!/bin/bash

sort -nr -t ':' -k3 /etc/passwd | cut -d: -f1,3
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Vistula University, Warsaw, Poland, Matusiak Sawomir, CII4SP16CI-L18

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

1 Like

You could pipe your results through an awk or sed program that prepends "uid=" to the second field, or replaces a space with " uid=". You could also do this with a "while read" shell loop.

From the given relevant commands I think the teacher or the course makers want to just cut one item from the passwd, process it in a for-do-done loop, and in the loop grep the item in the passwd again in order to get the second item.
But this is less efficient compared to piping the pair of items to awk or a while read loop.
The latter can directly split into variables if the field separator IFS is set accordingly. You do not even need to run cut before.

1 Like