How to write a UNIX script to send a mail to the respective individual users about their groups?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to write a UNIX script to send a mail to the respective individual users about their groups?
# 1  
Old 05-19-2017
How to write a UNIX script to send a mail to the respective individual users about their groups?

Hi Team,

I got a requirement to send a mail to the individual users of a unix server about their respective groups. can some one help me to provide the script as I am unable to write that.

I tried with below lines but I come out with errors.

Code:
cat /etc/passwd | awk -F':' '{ print $1}' | xargs -n1 groups

--- to get username and groups details
------------------------------------------------------------------
when i tried to send an mail to respective users it is throwing an error message as below:

Code:
syntax error near unexpected token `('

Code:
foreach user  ( `awk -F: '{ print $1 }' /etc/passwd ` ) 
    # Mail the file
    echo Mailing to $user
    mail -s "$subject" $user < $filename
   
    sleep 2
end

can some one can please help me with proper script.

Thanks in advance

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 05-19-2017 at 01:39 PM.. Reason: Added CODE tags.
# 2  
Old 05-19-2017
Moderator's Comments:
Mod Comment In addition to adding CODE tags, this thread has also been moved from a forum intended for contacting site administrators to a technical forum.

Hi harshabag,
What operating system and shell are you using?

It appears that you might be using the syntax for commands in one shell while executing commands with a different shell.
# 3  
Old 05-19-2017
Unix red hat OS, bash shell, can you please write some code as I am very new to this Unix.

Thanks in advance
# 4  
Old 05-20-2017
Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, and adequate (representative) sample input and desired output data and the logics connecting the two, to avoid ambiguities and keep people from guessing.

On my linux system running above with bash, I receive
Code:
bash: syntax error near unexpected token `('

So - please post entire error messages to help people help you.

In (a recent) bash, your groups list could be achieved like
Code:
groups $(awk -F':' '{ print $1}' /etc/passwd)

or
Code:
groups $(cut -d: -f1 /etc/passwd)

, and your loop modified to
Code:
for user in $(cut -d: -f1 /etc/passwd); do mail -s "$subject" $user   < <(groups $user); done

might work.

A slightly different approach would reduce disk I/O activity by running groups only once:
Code:
groups $(cut -d: -f1 /etc/passwd) | while read line; do echo $line | mail -s"subject" ${line%%:*}; done


Last edited by RudiC; 05-20-2017 at 01:49 AM..
# 5  
Old 05-20-2017
For something that would be more portable to a system where the groups utility only accepts one operand, the user database on your system is kept in the file /etc/passwd (which is not always true), and assuming that every user has a mail account with their login name as their mail address on that machine (which is not always true), you could also try:
Code:
cut -d: -f1 /etc/passwd | while read user
do	echo "Mailing to $user"
	groups "$user" | mail -s "$user's groups" "$user"
done

# 6  
Old 05-21-2017
Does the same works if we want to email the users mail id ?
# 7  
Old 05-22-2017
Hi Don,

Thanks for your valuable input's it worked well.

Regards,
Harsha.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write this script:- check output word and send a mail?

Hi Guys, I am not Good at scripting. I need to write a script such that if output of command shows the particular word in output then send mail to abc@compay.com -bash-3.2$ ps -ef | grep bpbkar root 6040 1 0 13:05:19 ? 0:00 bpbkar -r 2678400 -ru root -dt 47395 -to 0... (20 Replies)
Discussion started by: manalisharmabe
20 Replies

2. Shell Programming and Scripting

To get total number of users and their groups in remote UNIX machine

Hi All I am trying to do ssh to different server and on the remote server for each user trying to get groups of that user but i am not getting the required result. ssh username@ip_address "for i in $( cat /etc/passwd| cut -d: -f1);do groups $i done;exit" >>abc.txt only names are... (5 Replies)
Discussion started by: Ekamjot
5 Replies

3. Shell Programming and Scripting

Send a mail to multiple users

I have a file 1.txt which has 3 mail ids as below: Maillist=abc@gmail.com def@gmail.com rcg@gmail.com Now I want to write a script which will read this file and send the mail to all the users present in this file. (6 Replies)
Discussion started by: Vivekit82
6 Replies

4. Shell Programming and Scripting

Send e-mail to users

Dear all, The perl script to send e-mail is working in my current script. #This part of the script will send the email notification my $to='mohamed.rahman@noridian.com'; my $from='xyz@hotmail.com'; my $subject='POS_CODES_38 DATA LOADED SUCCESSFULLY.'; my $message='The total no. of files... (2 Replies)
Discussion started by: msrahman
2 Replies

5. UNIX for Dummies Questions & Answers

Finding out all users and their UNIX groups??

Is there a way to find out all users and the UNIX groups they belong to?? :) (3 Replies)
Discussion started by: Hangman2
3 Replies

6. UNIX for Dummies Questions & Answers

unix script to check if rsh to box and send status mail

rshstatus=`rsh -n lilo /db/p2/oracle/names9208/restart_names.sh` if $rshstatus <>0 then errstatus=1 mailx -s "xirsol8dr" ordba@xxx.com >> $log_dr else if errstatus=0 echo "status to xirsol8dr successful" can anyone provide if this is t he correct way to do this or is there a better way? (1 Reply)
Discussion started by: bpm12
1 Replies

7. Shell Programming and Scripting

script to send mail from unix??

Hi all, I wrote a shell script to send a mail, it is not showing any errors but i didn't receive any mail #!/bin/ksh . ./set_mail_details.ksh echo 'In script' subject=$1 echo '\nsubject' echo $1 echo "$subject" | sed 's/~/ /g' | read sub body_of_email=$2 echo '\nBody' echo $2... (7 Replies)
Discussion started by: deepaknbk
7 Replies

8. UNIX for Dummies Questions & Answers

Req the mail send from unix script

Hi , I want to send mail in my script when i am not a super user. Can some one help me on that .. Regards, Balamani (1 Reply)
Discussion started by: Balamani
1 Replies

9. Shell Programming and Scripting

Script to send a mail in UNIX

Hi, I need to write one unix script gor sending a mail notification. I have to pass the followinf as arguments,from ,to,subject,messege body Can i use mailx....Please provide the code Thanks in advance. (1 Reply)
Discussion started by: sudhi
1 Replies

10. UNIX for Dummies Questions & Answers

How to send mail to multiple users ???

Hi All, How to send mails to multiple users using a single mailx command. Urgently require help Thanks in advance Rgds Arunava (3 Replies)
Discussion started by: arunava_maity
3 Replies
Login or Register to Ask a Question