0

I have a bash script that is gathering active users on a machine and then I am going to curl away the data. The issue is that the first item in the list won't show up, it is gathering everything after the first. Can anyone explain why?

#!/bin/bash
users=$(ps -eo ruser,rgroup | grep users | sort | uniq | cut -d ' ' -f1)
while read -r users
do
   newVar=$newVar$(awk '{print "user_name{name=\""$users"\"}", 1.0}');
done <<< "$users"

Then I curl newVar which should be a concatenation of all users in the format that is required.

10
  • awk and read are both reading from the same standard input. Commented Jun 11, 2021 at 15:23
  • awk reads all of the input on the first iteration, so there's nothing left for the while loop to process. Commented Jun 11, 2021 at 15:25
  • Why are you using both a while loop and awk? It seems like you only need one loop. Commented Jun 11, 2021 at 15:26
  • Also, you never set the variable users inside awk. It's not the shell variable, since the awk script is in single quotes. Commented Jun 11, 2021 at 15:27
  • Can you show the desired result? Commented Jun 11, 2021 at 15:28

1 Answer 1

0

There's no need for the users variable or the while read loop. Just pipe the output of the ps pipe directly to awk.

You don't need cut, since awk can select the first column with $1. And sort | uniq can be combined into sort -u.

newVar=$(ps -eo ruser,rgroup | grep users | sort -u | awk '{printf ("username{name=\"%s\"} 1.0", $1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.