1

This might be a very basic question but I was not able to find solution. I have a script:

If I run w | awk '{print $1}' in command line in my server I get:

f931
smk591
sc271
bx972
gaw844
mbihk988
laid640
smk59
ycc951

Now I need to use this list in my bash script one by one and manipulate some operation on them. I need to check their group and print those are in specific group. The command to check their group is id username. How can I save them or iterate through them one by one in a loop.

what I have so far is

tmp=$(w | awk '{print $1})

But it only return first record! Appreciate any help.

5 Answers 5

3

Populate an array with the output of the command:

$ tmp=( $(printf "a\nb\nc\n") )
$ echo "${tmp[0]}"
a
$ echo "${tmp[1]}"
b
$ echo "${tmp[2]}"
c

Replace the printf with your command (i.e. tmp=( $(w | awk '{print $1}') )) and man bash for how to work with bash arrays.

For a lengthier, more robust and complete example:

$ cat ./tstarrays.sh
# saving multi-line awk output in a bash array, one element per line
# See http://www.thegeekstuff.com/2010/06/bash-array-tutorial/ for
# more operations you can perform on an array and its elements.

oSET="$-"; set -f       # save original set flags and turn off globbing
oIFS="$IFS"; IFS=$'\n'  # save original IFS and make IFS a newline

array=( $(
    awk 'BEGIN{
        print "the      quick   brown"
        print "    fox jumped\tover\tthe"
        print "lazy    dogs back    "
    }'
) )

IFS="$oIFS"             # restore original IFS value
set +f -$oSET           # restore original set flags

for (( i=0; i < ${#array[@]}; i++ ));
do
    printf "array[%d] of length=%d: \"%s\"\n" "$i" "${#array[$i]}" "${array[$i]}"
done

printf -- "----------\n"
printf -- "array[@]=\n\"%s\"\n" "${array[@]}"

printf -- "----------\n"
printf -- "array[*]=\n\"%s\"\n" "${array[*]}"

.

$ ./tstarrays.sh
array[0] of length=22: "the      quick   brown"
array[1] of length=23: "    fox jumped  over    the"
array[2] of length=21: "lazy    dogs back    "
----------
array[@]=
"the      quick   brown"
array[@]=
"    fox jumped over    the"
array[@]=
"lazy    dogs back    "
----------
array[*]=
"the      quick   brown     fox jumped  over    the lazy    dogs back    "

A couple of non-obvious key points to make sure your array gets populated with exactly what your command outputs:

  1. If your command output can contain globbing characters than you should disable globbing before the command (oSET="$-"; set -f) and re-enable it afterwards (set +f -$oSET).
  2. If your command output can contain spaces then set IFS to a newline before the command (oIFS="$IFS"; IFS=$'\n') and set it back to it's old value after the command (IFS="$oIFS").
Sign up to request clarification or add additional context in comments.

1 Comment

saving multi-line awk output in a bash array I was looking for that. Learned much.
2
tmp=$(w | awk '{print $1}')

while read i
do
    echo "$i"
done <<< "$tmp"

2 Comments

Note that the variable is not necessary. w | awk ... | while ... works just as well.
and you should always use while IFS= read -r i unless you have a specific modification of the input data you're trying to accomplish by not setting IFS (e.g. skipping leading blanks) and/or not using -r (e.g. interpolating backslashes).
1

You can use a for loop, i.e.

for user in $(w | awk '{print $1}'); do echo $user; done

which in a script would look nicer as:

for user in $(w | awk '{print $1}')
do
    echo $user
done

Comments

0

You can use the xargs command to do this:

w | awk '{print $1}' | xargs -I '{}' id '{}'

With the -I switch, xargs will take each line of its standard input separately, then construct and execute a command line by replacing the specified string '{}' in the command line template with the input line

Comments

0

I guess you should use who instead of w. Try this out,

who | awk '{print $1}' | xargs -n 1 id

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.