loop passwd in shell script - shell

how can i make a loop for users of server using shell script
i wrote this code ..
#!/bin/bash
b=`awk -F: '{ print $1 }' /etc/passwd | sort`
for $b in /home/$b/ /home/$b/ /home/$b/
echo "$b"
done
i want to loop all users and show its
the users like in file
/etc/passwd
like :
root,admin,cpanel,adm,mysql,user1,user2,user3,user4,user5
i want the output :
/home/adm
/home/root
/home/admin
/home/mysql
/home/user1
/home/user2
and thanks

To print /home/ before each user name:
$ awk -F: ' { print "/home/"$1 }' /etc/passwd | sort
/home/avahi
/home/backup
/home/bin
/home/clamav
/home/colord
/home/daemon
[...snip...]
If you want actual home directories, you can find them in field 6:
$ awk -F: ' { print $6 }' /etc/passwd | sort
/dev/null
/home/john1024
/nonexistent
/root
You can also access the sixth field using cut:
cut -d: -f6 /etc/passwd | sort
Printing multiple directories
As per the revised question in the comments:
$ awk -F: ' { p="/home/"$1; printf "%s\n%s\n%s\n",p"/www",p"/ftp",p"/etc" }' /etc/passwd | sort
/home/avahi/etc
/home/avahi/ftp
/home/avahi/www
/home/backup/etc
/home/backup/ftp
/home/backup/www
[...snip...]

Related

Grep command list bin/bash or dash

Is there a grep command that lists all users (in /etc/passwd) who use either bash (/bin/bash) or dash (/bin/sh) and their home directory is in /home (second to last section of: has home directory)?
I am thinking of commands:
getent passwd | awk -F ':' '$6 ~ "^/home" {print $1}'
or :
getent passwd| awk -F ":" '$7=="/bin/bash" { print $1 }'
but I am wrong.
Just combine your conditions:
getent passwd | awk -F':' '$7 ~ "^/bin/(ba)?sh$" && $6 ~ "^/home/"{ print $1 }'
or
getent passwd | awk -F':' '($7=="/bin/bash" || $7=="/bin/sh") && $6 ~ "^/home/"{ print $1 }'
The grep command that satisfies your conditions is as follows:
getent passwd | grep -E ':/home/.*?:.*?[bd]ash$'
The regex is all the lines with a home directory and bash or dash.

How to get the second word of a string in shell?

I want to get the size of the directory's content. If I use the command line I can get like this:
ls -l | head -n 1 | awk '{ print $2 }'
So the output is the size of the directory's content:
16816
But I want to do this in a bash script:
x="ls -l DBw | head -n 1 | awk '{ print $2 }'"
while sleep 1; do
y=$(eval "$x");
echo $y
done
But the output of this script is the full line:
Total 16816
Why is this happening and how can I get just the second word?
x="ls -l DBw | head -n 1 | awk '{ print $2 }'"
It's happening because $2 is inside double quotes and so it's interpreted immediately. You could escape it with \$2, but better yet:
Don't store code in variables. Use a function.
x() {
ls -l DBw | head -n 1 | awk '{ print $2 }'
}
Then you can call x many times.
while sleep 1; do
x
done
That said, it's better not to parse the output of ls in the first place. Use du to compute the size of a directory:
$ du -sh /dir
1.3M /dir
$ du -sh /dir | cut -f1
1.3M

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)

Adding value to global variable in a subshell is not working

I am trying to get the total disk usage of my machine. Below is the script code:
#!/bin/sh
totalUsage=0
diskUse(){
df -H | grep -vE '^Filesystem|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
diskUsage=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
totalUsage=$((totalUsage+diskUsage))
done
}
diskUse
echo $totalUsage
While totalUsage is a global variable, I have tried to sum the individual disk usage to totalUsage in the line:
totalUsage=$((totalUsage+diskUsage))
An echo of totalUsage between do and done shows the correct value,
but when I try to echo it after my call diskUse, it stills prints a 0
Can you please help me, what is wrong here?
The variable totalUsage in a sub-shell doesn't change the value in the parent shell.
Since you tagged bash, you can use here string to modify your loop:
#!/bin/bash
totalUsage=0
diskUse(){
while read output;
do
diskUsage=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
totalUsage=$((totalUsage+diskUsage))
done <<<"$(df -H | grep -vE '^Filesystem|cdrom' | awk '{ print $5 " " $1 }')"
}
diskUse
echo $totalUsage
I suggest to insert
shopt -s lastpipe
as new line after
#!/bin/bash
From man bash:
lastpipe: If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment.

Running last in awk [Solaris]

How can i run last command for every matched line in awk?
ypcat passwd | awk -F":" '/:John / { system("last" $1) }'
I'm trying to execute the last command for every user that is named John but It does not print anything.
Insert a whitespace after last:
ypcat passwd | awk -F":" '/:John / {system("last " $1)}'
Your approach is wrong, awk is not shell. awk is designed to manipulate text not to call other tools from, that is what a shell is for This may be what you want, depending on what last is/does:
ypcat passwd | awk -F":" '/:John /{print $1}' | xargs last
or:
ypcat passwd | awk -F":" '/:John /{print $1}' | xargs -n 1 last

Resources