How - given USER ID as parameter, find out what is his name? The problem is to write a Bash script, and somehow use etc/passwd file.
Bash Script - get User Name given UID
3.7k Views Asked by PaulW At
2
There are 2 best solutions below
0
Pedro Lobito
On
The uid is the 3rd field in /etc/passwd, based on that, you can use:
awk -v val=$1 -F ":" '$3==val{print $1}' /etc/passwd
4 ways to achieve what you need:
http://www.digitalinternals.com/unix/linux-get-username-from-uid/475/
Related Questions in BASH
- How do I recursively find and replace only in files named index.php on Linux webserver?
- Delete the extra space after special character in all the lines of text file
- Calling a python function with options from shell script
- bc: prevent "divide by zero" runtime error on multiple operations
- Multiple commands with find and xargs, also accounting for special characters
- How to split a directory into parts without compressing or archiving?
- concat a lot of files to stdout
- Honoring quotes while reading shell arguments from a file
- No laravel sync folders in homestead vagrant on windows
- Grouping commands in curly braces and piping does not preserve variable
- SWI Prolog pass a goal with non-zero arity through the command line arguments
- Evaluating condition of if statement in awk using a second file
- How to customise bash completion to pick only a custom set of commands?
- Bash regular expression execution hangs on long expressions
- Bitwise OR in bash arguments with square brackets
Related Questions in UNIX
- passing text with \n as one argument in shell
- C std library don't appear to be linked in object file
- How to split a directory into parts without compressing or archiving?
- Momentjs get current GMT unix time
- Timing packets on a traffic server
- man pages for c variable types
- Blocking in pthread_join()
- PWX-00001 Error opening repository "dtlmsg.txt". RCs = 268/150/2
- Unix c program to calculate pi using threads
- How to perform parallel processes for different groups in a folder?
- Set aliases globally for all users
- wmic csproduct get UUID equivalent for Unix and Mac?
- Send alert for 80% threshold comparing two values from Disk partition
- Unix - Tail Utility would open the file or not
- Redirect Outward of unix os commands to html page
Related Questions in UID
- Create a folder in Plone and set uid
- Obtaining Client UID for login
- Bash Script - get User Name given UID
- How to get owner:group name of a file/directory with perl?
- what does it mean when uid equals to zero for Android system
- What is the GID/UID of the system on Android?
- firebase : unable to user authorization data is undefined, but uid is retrieving correctly Node.js
- generate a 5 char unqiue key for an iphone app
- Persistent UIDs for a mailbox
- sudo: effective uid is not 0, is sudo installed setuid root?
- setuid bit on, yet program can't open a superuser file
- How do I create a unique identifier that does not contain numbers?
- Making secure authentications in firebase
- Which is the data type to use for storing a firebase uid in postgresql
- Error while accessing current uid of firebase user in flutter
Related Questions in ETCPASSWD
- Bash Script - get User Name given UID
- I need to grep for an exact word in password file but I get two responses back
- Modifying correct group name and ID within /etc/group file
- How do I delete users that have uid 0?
- How do I extract the biggest UID value from /etc/passwd?
- How to read awk from one file and compare it with awk from another file
- ./script.sh: line 8: /etc/passwd: Permission denied
- Separate first name with middle name and last name with strtok_r
- Unix [Homework]: Get a list of /home/user/ directories in /etc/passwd
- If conditional using Awk to search for exact Bash variable string
- vulnerability check on Ubuntu 18.04 Bash Shell Script
- Open a terminal ends in an infinte loop
- Getting length of single string in two-dimensional dynamic allocated array of strings
- How do I find all "last names" which are shared by 5-10 users in an /etc/passwd file?
- Parsing /etc/passwd with regex_*, unstandard behavior C++
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Try this:
grep ":$1:" /etc/passwd | cut -f 1 -d ":"This greps for the UID within /etc/passwd.
Alternatively you can use the getent command:
getent passwd "$1" | cut -f 1 -d ":"It then does a cut and takes the first field, delimited by a colon. This first field is the username.
You might find the SS64 pages for cut and grep useful: http://ss64.com/bash/grep.html http://ss64.com/bash/cut.html