Print the name and home directory path associated with a username - bash

I need to ask for a username and print the name and home directory path associated with that username
echo Please enter your username
read username
I am not sure how to use $username to locate information regarding its home directory and name.

awk -F: -v v="$username" '{if ($1==v) print $6}' /etc/passwd
Without awk:
cat /etc/passwd | grep "$username" | cut -d ":" -f6
User name:
cat /etc/passwd | grep "$username" | cut -d ":" -f5

echo Please enter a username
read username
cat /etc/passwd | grep "$username" | cut -d ":" -f6
cat /etc/passwd | grep "$username" | cut -d ":" -f5

use getent
read -p "username: " username
IFS=: read username password uid gid gecos homedir shell < <(getent passwd "$username")
echo "fullname=${gecos%%,*}"
echo "homedir=$homedir"

Related

How to create username from txt file

I am trying to create a user from a a text file called names.txt
(first and second field will be comment, third field will be username and forth field will be password)
Jessica Brown,jessicabrown,id0001
James Santos,jamessantos,id0002
This is what I have so far, but it is not working and I have a feeling I can do it a shorter way but can't figure it out.
user_name=$(cat names.txt | cut -d, -f3)
password1=$(cat names.txt | cut -d, -f4)
comment1=$(cat names.txt | cut -d, -f1 -f2)
user_name2=$(cat names.txt | cut -d, -f3)
password2=$(cat names.txt | cut-d, -f4)
comment2=$(cat names.txt | cut -d, -f1 -f2)
useradd "$user_name" -p "$password1" -c "$comment1"
useradd "$user_name2" -p "$password2" -c "$comment2"
To read the file and split the fields you may simply use read-while loop
Check man useradd for proper options for the command
Example (echo is here only for demonstration, remove it in final script):
#!/usr/bin/env bash
while IFS="," read -r COMMENT1 COMMENT2 USERNAME PASSWORD ; do
echo useradd "$USERNAME" --password "$PASSWORD" --comment "$COMMENT1,$COMMENT2"
done <names.txt

How to find list of groups that are present in '/etc/passwd' but not in the '/etc/group'?

I want to find list of groups that are present in /etc/passwd but not in the /etc/group
I have written the following command so far
for user in $(getent passwd | cut -d: -f1); do
printf "%s: %s\n" $user "$(id -nG $user)"
done
$ id -Gz | cat -v
197121^#114^#197610^#544^#545^#4^#66049^#11^#15^#113^#4095^#66048^#262154^#405504^#$
$ getent passwd | tr '[:alpha:]' '#'
########:*:197609:197121:#-#######53\########,#-1-5-21-2486228713-2700429697-662227502-1001:/####/########:/###/####
##############:*:544:544:#-#######\##############,#-1-5-32-544:/:/####/#######
######:*:18:18:#-## #########\######,#-1-5-18:/####/######:/###/####
##### #######:*:19:19:#-## #########\##### #######,#-1-5-19:/:/####/#######
####### #######:*:20:20:#-## #########\####### #######,#-1-5-20:/:/####/#######
##############:*:544:544:#-#######\##############,#-1-5-32-544:/:/####/#######
## #######+################:*:328384:328384:#-## #######\################,#-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464:/:/####/#######
$ awk 'NR==FNR{grps[$0];next} !($3 in grps){print $3}' RS='\0' <(id -Gz) RS='\n' FS=':' <(getent passwd)
197609
18
19
20
328384
Could you please try following, where it will give entries of those group names which are present in /etc/passwd and NOT in /etc/group.
awk -F':' 'FNR==NR{a[$5]=$0;next} ($1 in a){delete a[$1]} END{for(i in a){print a[i]}}' /etc/passwd /etc/group
with grep/cut
$ grep -vxFf <(cut -d: -f1 </etc/group) <(cut -d: -f1 </etc/passwd)

generate different password for each usr in list

I use the following bash script to change password for users in list:
pass=`cat /dev/urandom | tr -dc 'a-zA-Z0-9-_!##$%^&*()_+{}|:<>?=' | fold -w 12 | grep -i '[!##$%^&*()_+{}|:<>?=]' | head -n 1`
for usr in `cat usrs.lst`
do
printf "Username is $usr and password is $pass\n"
done
The problem with the previous code it that doesn't change the password
meaning it gives one password for every user.
I need the previous script to give a different password for each user
so what am I missing in the previous code?
you should assign pass variable in loop as below;
#!/bin/bash
while IFS= read -r usr; do
pass=$(tr -dc 'a-zA-Z0-9-!##$%^&*()+{}|:<>?=' < /dev/urandom | fold -w 12 | grep -i '[!##$%^&*()_+{}|:<>?=]' | head -n 1)
printf 'Username is %s and password is %s\n' "$usr" "$pass"
done < usrs.lst

A generic script for checking the health of application

I have written one shell script (Health_app.sh) which checks the health of the application. And for that it takes the name of the processes from App_Details file and checks for PID (whether it is running or not) and if it is not running and grep for that process in logs (field 3) and send email to the email id mentioned in the App_Details file (field 4).
App_Details is having records like:
process_Name|Process_description|logfile_path|email
abcd|main proceess to invoke the
dataready|/123/456/log|vikas#yahoo.com
pqrs|2nd
process..........................|/123/456/log|vikas#yahoo.com
Here is how my script looks like:
export App_Details=/home/123/sanity/App_Details
while read line
do
export procname=$(echo $line | cut -d " " -f1)
export PROCDES=$(echo $line | cut -d " " -f2)
#if ps -ef |grep [`echo $procname|awk '{print substr($0,1,1)}'`] [`echo $procname|awk '{print substr($0,2,length($0))}'`]> /dev/null
if ps -ef |grep -q [`echo $procname|awk '{print substr($0,1,1)}'`] `echo $procname|awk '{print substr($0,2,length($0))}'`
then
export part1=[`echo $procname|awk '{print substr($0,1,1)}'`]
export part2=`echo $procname|awk '{print substr($0,2,length($0))}'`
export PROCID=`ps -ef |grep $part1$part2|awk -F ' ' '{print $2}'`
else
export PROCID="OFFLINE"
trace_path=$(echo $line | cut -d " " -f3)
export mail=$(echo $line | cut -d " " -f4)
file_name=`ls -rt $trace_path/$procname*.trc 2>/dev/null | tail -1`
#export PROCDES=$(echo `tail -10 $file_name`)
(echo `tail -10 $file_name`) >> send.txt
mailx -s "Please find the alerts for your application OFFLINE services" vikas#domain.com < send.txt
fi
echo $PROCID|awk '{ printf("%-20s", $0)}'
echo $procname|awk '{ printf("%-20s", $0)}'
echo $PROCDES|awk '{ printf("%-20s\n", $0)}'
done<$App_Details
Now the issue is that grep -q is illegal for solaris and it is not working in solaris server.

bash script extract domain name for LDAP

I am currently working on a script that asks the user entered a domain name. This domain name is used to fill the hqbase variable for LDAP.
For example, if the user enter "example.com", i have to cut the domaine name with example on the side and "com" another side.
I found this example:
echo http://example.com/index.php | awk -F/ '{print $3}'
But how to split example from com (any ".").
You want . as your delimiter:
# Using cut
$ echo "domain.com" | cut -d. -f1
domain
$ echo "domain.com" | cut -d. -f2
com
# Using awk
$ echo "domain.com" | awk -F. '{print $1}'
domain
$ echo "domain.com" | awk -F. '{print $2}'
com
# Save values
$ first=$(echo "domain.com" | cut -d. -f1)
$ second=$(echo "domain.com" | cut -d. -f1)

Resources