How to get the logged in user's real name in Unix? - unix

I'm looking to find out the logged in user's real (full name) to avoid having to prompt them for it in an app I'm building. I see the finger command will output a columned list of data that includes this and was wondering if it makes sense to grep through this or is there an easier way? None of the switches for finger that I've found output just the real name. Any thoughts would be much appreciated.

getent passwd `whoami` | cut -d : -f 5
(getent is usually preferable to grepping /etc/passwd).

getent passwd "$USER" | cut -d: -f5 | cut -d, -f1
This first fetches the current user's line from the passwd database (which might also be stored on NIS or LDAP)
In the fetched line, fields are separated by : delimiters. The GECOS entry is the 5th field, thus the first cut extracts that.
The GECOS entry itself is possibly composed of multiple items - separated by , - of which the full name is the first item. That's what the second cut extracts. This also works if the GECOS entry is lacking the commas. In that case the whole entry is the first item.
You can also assign the result to a variable:
fullname=$( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1 )
Or process it further directly:
echo "$( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1 )'s home is $HOME."
cat <<EOF
Hello, $( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1 ).
How are you doing?
EOF

You can use getpwent() to get each successive password entry until you find the one that matches the currently logged in user, then parse the gecos field.
Better, you can use getpwuid() to directly get the entry for the uid of the current user.
In either case,
You have to first get the current user's login name or id, and
There is no guarantee that the gecos field actually contains the user's real full name, or anything at all.

Specific to macOS, there is no getent command; instead you have to use id -F

For macOS and Linux:
if [ "Darwin" = $(uname) ]; then
FULLNAME=$(id -P $USER | awk -F '[:]' '{print $8}')
else
FULLNAME=$(getent passwd $USER | cut -d: -f5 | cut -d, -f1)
fi
echo $FULLNAME

I use
grep "^$USER:" /etc/passwd | awk -F: '{print $5}'
Explanations:
$USER contains the login of the current user
The first part (grep) extract from /etc/passwd the line about that user
The second part (awk) splits this line with separator ':' and prints the 5th component,
which is the full name
If you don't want to rely on $USER being set, you can use that instead:
grep "^`whoami`:" /etc/passwd | awk -F: '{print $5}'

How about trying whoami or logname

Related

Get a list of unique sender(from=) domains in postfix maillog

I am currenlty trying to extract all the sender domains from maillog. I am able to do some of that with the below command but the output is not quite what I desired. What would be the best approach to retrieve a unique list of sender domain from maillog?
grep from= /var/log/maillog | awk '{print $7}' | sort | uniq -c | sort -n
output
1 from=<user#test.com>,
1 from=<apache#app1.com>,
2 from=<bounceld_5BFa-bx0p-P3tQ-67Nn#example.com>,
2 from=<bounceld_19iI-HqaS-usVU-fqe5#example.com>,
12 reject:
666 from=<>,
desired output
test.com
app1.com
example.com
See useless use of grep; if you are using Awk anyway, you don't really need grep at all.
awk '$7 ~ /from=.*#/{split($7, a, /#/); ++count[a[2]] }
END { for(dom in count) print count[dom], dom }' /var/log/maillog
Collecting the counts in an associative array does away with the need to call sort and uniq, too. Obviously, if you don't care about the count, don't print count[dom] at the end.
This should give you the answer:
grep from= /var/log/maillog | awk '{print $7}' | grep -Po '(?=#).{1}\K.*(?=>)' | sort -n | uniq -c
... change last items to "| sort | uniq" to remove the counts.
References:
https://www.baeldung.com/linux/bash-remove-first-characters {1}\K use
Extract email addresses from log with grep or sed -Po grep function

grep multiple files get count of unique cut

I think I'm close on this, and saw similar questions but couldn't get it to work as I want. So, I have several log files and I would like to count the occurrences of several different service calls by date.
First I tried the below, the cut is just to get the first element (date) and 11th element (name of service call), which is specific to my log file:
grep -E "invoking webservice" *.log* | cut -d ' ' -f1 -f11 | sort | uniq -c
But this returned something that looks like:
5 log_1.log:2017-12-05 getLegs()
10 log_1.log:2017-12-05 getArms()
7 log_2.log:2017-12-05 getLegs()
13 log_2.log:2017-12-04 getLegs()
What I really want is:
12 2017-12-05 getLegs()
10 2017-12-05 getArms()
13 2017-12-04 getLegs()
I've seen examples where they cat * first, but looks like the same problem.
cat * | grep -E "invoking webservice" *.log* | cut -d ' ' -f1 -f11 | sort | uniq -c
What am I doing wrong? As always, thanks a lot!
Your issue seems to be that grep prefixes the matched lines with the filenames. (grep has this behavior when multiple filenames are specified, to disambiguate the results.) You can pass the -h to grep to not print the filenames:
grep -h "invoking webservice" *.log | cut -d ' ' -f1 -f11 | sort | uniq -c
Note that I dropped the -E flag, because it is used to enable extended regex support, and your example doesn't need it.
Alternatively, you could use cat to dump the content of files to standard output, and pipe that to grep. That would work, because it removes the need for filename parameters for grep:
cat *.log | grep "invoking webservice" | cut -d ' ' -f1 -f11 | sort | uniq -c

set password for user account from a fileun issue

I want to create user account n times in UNIX
after creating the account I want to set password reads line by line from a file called password.txt
but I do not know how to do that!
my bash script contains this so far:
sudu useradd user
passwd
I have stuck on this point !, I do not know how to set password from a file?
what command should I use?
I want something like this but I want to read pass word not user name
for i in `more pass.txt `
do
echo $i
adduser $i
done
Have the code like this
#!/bin/bash
for i in `cat unpw.csv`; do
UN=`echo $i | cut -f1 -d','`
PW=`echo $i | cut -f2 -d','`
ENCPW=`echo $PW | mkpasswd -s`
echo useradd -p $ENCPW -m $UN
done
where the input file unpw.csv has
user1,password1
user2,password2
user3,password3
PS: Make sure that mkpasswd is installed

using awk to get column values and then running another command on values and printing them

I've always used Stack Overflow to get help with issues but this is my first post. I am new to UNIX scripting and I was given a task to get values of column two and then run a command on them. The command I am suppose to run is 'echo -n "$2" | openssl dgst -sha1;' which is a function to hash a value. My problem is not hashing one value, but hashing them all and then printing them. Can someone maybe help me figure this out? This is how I am starting but I think the path I am going is wrong.
NOTE: this is a CSV text file and I know I need to use AWK command for this.
awk 'BEGIN { FS = "," } ; { print $2 }'
while [ "$2" != 0 ];
do
echo -n "$2" | openssl dgst -sha1
done
This prints the second column in it's entirety and also print some type of hashed value.
Sorry for the long first post, just trying to be as specific as possible. Thanks!
You don't really need awk just for extracting the second column. You can do by using bash read built in and setting the IFS to the delimiter.
while IFS=, read -ra line; do
[[ ${line[1]} != 0 ]] && echo "${line[1]}" | openssl dgst -sha1
done < inputFile
You should probably post some sample input data and the error you are getting so that someone can debug your existing code better.
This will do the trick:
$ awk '{print $2}' file | xargs -n1 openssl dgst -sha1
Use awk to print the second field in the file and xargs with the -n1 to pass each record separately to openssl.
If by CSV you mean each record is seperated by a comma then you need to add -F, to awk.
$ awk -F, '{print $2}' file | xargs -n1 openssl dgst -sha1

sorting ls-l owners in Unix

I want to sort the owners in alphabetical order from a call to ls -l and cannot figure out a way to do it. I know something like ls-l | sort would sort the file name but how do i sort the owners in order?
The owner is the third field, so use -k 3:
ls -l | sort -k 3
You can extend this idea to sorting based on other fields, and you can have multiple -k options. For instance, maybe you want to sort by owner, and then size in descending order:
ls -l | sort -k 3,3 -k 5rn
I am not sure if you want only the owners or the whole information sorted by owner. In the former case superfo's solution is almost correct.
Additionally you need to remove repeating white spaces from ls's output with tr because otherwise cut that uses them as a delimiter won't work in all directories.*
So in the end you get this:
ls -l | tr -s ' ' | cut -d ' ' -f 3 | sort | uniq
*Some directories have a two digit value in the second field and all other lines with a single digit get an additional whitespace to preserve the layout.
How about ...
ls -l | cut -d ' ' -f 3 | sort | uniq
Try this:
ls -l | awk '{print $3, $4, $8}' | sort
It will print the user name, the group name and the file name. (File name cannot contain spaces)
ls -l | awk '{print $3, $4, $0}' | sort
This will print the user name, group name and the full ls -l output, sorted by the user name first, then the group name, then what ls -l prints first

Resources