The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #7 (permalink)  
Old 05-23-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Code:
set -- `users`
var2=" "
for f; do
  case $var2 in *" "$f" "*) ;; *) var2=" $f$var2";; esac
done
This should work as long as the output you want to process is whitespace-separated. You can play around with IFS= to use a different separator, but in the general case, it gets rather ugly and cumbersome.

For convenience, I made it so that $var2 will have one leading and one trailing space. Removing or ignoring those is left as an exercise.

The shell is not very good at these things; that's why awk and sed are more or less essential for nearly any moderately complex shell script. (Or use Perl if that helps at all.)

Modern shells have additional facilities, including an array variable type, but this approach works even with Bourne Classic. We are stuck with it, and in fact, we kind of like it, in spite of its oddities and quirks.