1

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

saba
  • 35
  • 1
  • 10
  • Is your goal to find all the home directories or to print `/home/username`? For example, in standard unix, root's home directory is usually `/root`, not `/home/root`. – John1024 Aug 21 '15 at 05:56

1 Answers1

0

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...]
John1024
  • 109,961
  • 14
  • 137
  • 171
  • thanks sir so nice but i want to test and loop each user of users in all these paths in tha same time /home/user1/www /home/user1/ftp /home/user1/etc /home/user2/www /home/user2/ftp /home/user2/etc /home/user3/www /home/user3/ftp /home/user3/etc how can i do that ? and thanks – saba Aug 21 '15 at 06:30
  • @saba I updated the answer to print multiple directories for each user. – John1024 Aug 21 '15 at 06:42
  • thanks so much mr.john .. you are cool .. thanks .. best regards for you and thanks for stackoverflow.com – saba Aug 21 '15 at 09:48
  • sir john i have a questions please check my questions please – saba Aug 21 '15 at 12:48