Script to run #groups command on list in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to run #groups command on list in a file
# 1  
Old 06-28-2019
Script to run #groups command on list in a file

I have to provide a listing of all usernames and group assignments on Linux servers running SLES 12 SP 3. I am trying to automate this via a #for loop. It is not doing as desired and could use a little assistance. Here is the code:
Code:
#!/bin/bash

for uname in 'cat $(hostname)_unlist.txt'
	do
		echo $uname >> $(hostname)_uglist.txt
		"groups $uname"$'\n' >> uglist.txt
	done

I am still very new to UNIX but have a mild programming background. Just not sure where I am failing here. In the file I am hoping to have the $uname variable then the output from the #groups command followed by a new line input.
# 2  
Old 06-28-2019
Hi,

I think you're very nearly there, but perhaps have just over-complicated things a bit. You can do all this with just the one input file, the one output file, and the groups command.

Here's my proposed solution. I've assumed that the input file simply contains usernames listed one per line (since there's no sample input to go on in your post) Other than that, I believe this should do what you need.

Code:
$ cat $(/bin/hostname)_unlist.txt
pulse
root
unixforum
$ cat script.sh 
#!/bin/bash

for uname in `/bin/cat $(/bin/hostname)_unlist.txt`
do
        groups=`/usr/bin/groups "$uname" | /usr/bin/awk -F": " '{print $NF}'`
        echo "$uname","$groups" >> uglist.txt
done

$ ./script.sh 
$ cat uglist.txt
pulse,pulse audio
root,root bin daemon sys adm disk wheel audio
unixforum,users
$

So the basic idea is that for each username we run the groups command to get the list of UNIX groups for that user, then use awk to just print out the part of the output of the groups command that we need. We then write the username and our list of groups to a single line in the output file, and move on to the next user until there is no input left to read.

Hope this helps ! If not, then if you can clarify what aspects of this script don't fit with your expectations in terms of behaviour or output, then we can take things from there.
# 3  
Old 06-28-2019
That still doesn't run correctly. Here is the output (edited for security):

Code:
<me> :~> ls -l
total 36
drwxr-xr-x 2 <me>  users 4096 Dec  3  2018 bin
-rw-r----- 1 <me>  users   34 Jun 28 09:14 (hostname)_unlist.txt
-rw-r----- 1 <me>  users  109 Jun 28 09:12 (hostname)_userlist.txt
-rwxr----- 1 <me>  users  207 Jun 28 11:00 groups.sh
-rw-r----- 1 <me>  users  145 Apr 25 09:02 hello.h
-rw-r----- 1 <me>  users  254 Apr 26 13:37 intro
-rw-r----- 1 <me>  users  234 Dec 28 09:16 SLES.txt
-rw-r----- 1 <me>  users    0 Jan 17 15:34 su
-rw-r----- 1 <me>  users   78 Dec 28 11:05 testsles.txt
-rw-r----- 1 <me>  users  216 Jun 28 09:08 unlist.txt
x:~> groups
users
x:~> ls -l
total 36
drwxr-xr-x 2 <me>  users 4096 Dec  3  2018 bin
-rw-r----- 1 <me>  users   34 Jun 28 09:14 (hostname)_unlist.txt
-rw-r----- 1 <me>  users  109 Jun 28 09:12 (hostname)_userlist.txt
-rwxr----- 1 <me> users  207 Jun 28 11:00 groups.sh
-rw-r----- 1 <me>  users  145 Apr 25 09:02 hello.h
-rw-r----- 1 <me>  users  254 Apr 26 13:37 intro
-rw-r----- 1 <me>  users  234 Dec 28 09:16 SLES.txt
-rw-r----- 1 <me>  users    0 Jan 17 15:34 su
-rw-r----- 1 <me>  users   78 Dec 28 11:05 testsles.txt
-rw-r----- 1 <me>  users  216 Jun 28 09:08 unlist.txt

# 4  
Old 06-28-2019
Hi,

I'm not quite sure what could have gone wrong, since I'm not seeing any sign of you having run the script or the output file being created here. If you run the version of the script that I supplied in the previous response, what exactly do you get as the output in your terminal ? Does the format of the input conform to my assumptions ? If not, then you'd need to modify the script accordingly to fit whatever format your input is actually in.
# 5  
Old 06-28-2019
Here is what I get when I try to run it like you typed it.

Code:
klu@wwvd1a:~> ./groups.sh
-bash: ./groups.sh: /bin/bash^M: bad interpreter: No such file or directory

# 6  
Old 06-28-2019
Hi,

This would to me indicate that something has gone wrong when you copied-and-pasted the script code into your terminal. The ^M character is used by Windows to mark the end of lines in a file, and is not used by UNIX-style OS's by and large. So you've somehow ended up with Windows-style EOL characters embedded in the script here, which your server is treating as part of the shell filename. If you can strip those out or get this script onto your server in a different way it should work (if, as i say, the input assumptions I've made are correct).
# 7  
Old 06-28-2019
Seems like your editor inserted DOS line terminators (^M = <CR> = \r = 0x0D). Remove and retry.


But - why that intricate? Try just (assuming a recent bourne compatible shell providing "command substitution")

Code:
groups $(< (hostname)_unlist.txt)

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

3. Shell Programming and Scripting

Script to read through a file and create new users/assign them to groups in Ubuntu

Hi all. I need a shell script that can, in short, read through a text file line by line and create a new user in Ubuntu, as well as assign that user to a group. The format of the text file is not important but preferably: 'username:group'. I don't have much programming knowledge no matter shell... (3 Replies)
Discussion started by: LewisWeekly
3 Replies

4. UNIX for Dummies Questions & Answers

Read workstation list from file und run command

Hi, how do I read in a file which includes a list of workstations and then run a command for each workstation ? I am unclear which command to use to read in , or is this not possible ? Thanks, (3 Replies)
Discussion started by: manni2
3 Replies

5. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

6. Shell Programming and Scripting

Urgent...Need a shell script to list files which belong to particular groups

Hi, I am just new to scripting but got to write a complex scipt please help. i need a shell script which can check the list of data listed in a txt doc and see if they belong to any of the groups that are listed in other list file.... (5 Replies)
Discussion started by: draghun9
5 Replies

7. Shell Programming and Scripting

how to run an already made script run against a list of ip addresses solaris 8 question

how to run an already developed script run against a list of ip addresses solaris 8 question. the script goes away and check traffic information, for example check_GE-VLANStats-P3 1.1.1.1 and returns the results ok. how do I run this against an ip list? i.e a list of 30 ip addresses (26 Replies)
Discussion started by: llcooljatt
26 Replies

8. Shell Programming and Scripting

Script to run command against multiple specific value in one file

Hi all, I am trying to create a shell script from solaris 10 server to run a command into multiple specific value in one file. The command is related to Oracle/Sun JES2005Q4 directory server. #this is the command, #from path /jes/ds/slapd-rldap1 ./ns-inactivate.pl -h mldap1 -p 389 -D... (12 Replies)
Discussion started by: Mr_47
12 Replies

9. Shell Programming and Scripting

Run SQL command for a list of devices

Hi Please help me to resolve. Question: I can run this command to change the mode of a device with id=500 as below dbc "select device mode=3 where id=500;" How can i run the same query with a file contaning n number of ids ? file1.txt 12 234 34 500 34 45 Thanks in... (3 Replies)
Discussion started by: sureshcisco
3 Replies

10. Shell Programming and Scripting

script to run shell command and insert results to existing xml file

Hi. Thanks for any help with this. I'm not new to programming but I am new to shell programming. I need a script that will 1. execute 'df -k' and return the volume names with specific text 2. surround each line of the above results in opening and closing xml tags 3. insert the results of step... (5 Replies)
Discussion started by: littlejon
5 Replies
Login or Register to Ask a Question