Unix/Linux Go Back    


Homework & Coursework Questions Students must use and complete the template provided. If you do not use the template your post may be deleted! Special homework rules apply here.

Display usernames and their UIDs

Homework & Coursework Questions


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by baniel
baniel's Unix or Linux Image
baniel baniel is offline
Registered User
 
Join Date: May 2018
Last Activity: 27 May 2018, 2:17 PM EDT
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
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
... ...

2. Relevant commands, code, scripts, algorithms:

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

3. 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 Linux
Since I don't have a Solaris or Linux on PC, I've tested the code on tutorialspoint/execute_ksh_online.



Code:
#!/bin/bash

sort -nr -t ':' -k3 /etc/passwd | cut -d: -f1,3

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Vistula University, Warsaw, Poland, Matusiak Sławomir, 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).
The Following User Says Thank You to baniel For This Useful Post:
dodona (3 Weeks Ago)
Sponsored Links
    #2  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by baniel
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 23 June 2018, 4:01 PM EDT
Location: Amsterdam
Posts: 11,836
Thanks: 545
Thanked 3,455 Times in 3,045 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by baniel
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 22 June 2018, 6:48 PM EDT
Location: Simplicity
Posts: 4,124
Thanks: 359
Thanked 1,403 Times in 1,260 Posts
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.
The Following User Says Thank You to MadeInGermany For This Useful Post:
dodona (3 Weeks Ago)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
UIDs in /etc/passwd file vvpotugunta Solaris 10 04-10-2009 04:20 PM
keep UIDs/GIDs consistent robertngo UNIX for Advanced & Expert Users 4 03-11-2009 03:00 AM
UIDs being overwritten immediately xsys2000 AIX 2 05-08-2007 01:00 PM
Valid ranges for uids for HP-UX naren_chella HP-UX 7 06-25-2006 10:22 PM
about UIDs, very urgent mehdi UNIX for Dummies Questions & Answers 1 09-06-2005 07:53 PM



All times are GMT -4. The time now is 04:10 PM.