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:

#!/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.

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.

$ 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.

That still doesn't run correctly. Here is the output (edited for security):

<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

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.

Here is what I get when I try to run it like you typed it.

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

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).

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")

groups $(< (hostname)_unlist.txt)
1 Like

@RudiC

That was perfect. I just had to insert the variable identifier $ in front of (hostname) in the command. I even was able to redirect it to my file.

THANK YOU

--- Post updated at 06:02 PM ---

@RudiC

That was exactly the short command I needed. I did have to add a variable indicator $ in front of the (hostname) in the command, but the output is perfect.

THANK YOU.

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

In theory the groups command can overflow with "two many arguments".

The xargs command can avoid it, would rather start another groups command with the remaining arguments.

xargs groups < $(hostname)_unlist.txt