Find out the System users

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    I want to create a file called Allusers that contains the list of
    currently logged on users and the total number of them.
    (note: it should print, The Total is: , at the end of the Allusers file)
    I have made the command, and when i perform it , it show me all the users and it print the total of them. But my problem that the command doesn't save the result in the Allusers file.

  2. Relevant commands, code, scripts, algorithms:

who
sort
tee
cat
echo
cat
wc

  1. The attempts at a solution (include all code and scripts):

who | sort -t: -k1 | tee All |cat; echo Total; cat All |wc -l; rm All > Allusers

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

University of Newengland, Armidale, NSW, Australia
Dr.Paul Kwan, COMP170.

If you want to have both written to the screen and written to the file, try this:

FILE=Allusers; MSG="Total is:"; who| cut -d " " -f1| sort| tee $FILE; echo $MSG; echo $MSG >> $FILE; who| wc -l| tee -a $FILE

If it is ok to just have it all in the file and list it's content with a cat :

$> FILE=Allusers; MSG="Total is:"; who| cut -d " " -f1| sort > $FILE; echo $MSG >> $FILE; who| wc -l >> $FILE

I used variables, since I am too lazy to write everything again and again and can change variable content at 1 point, not at all points where it is used in the code.

Issuing a who gives out a line with terminal information and maybe remote host info like:

root     pts/0        2011-03-14 10:09 (somehost.somedomain.org)

If you want just the user without any other information, so that cut is not needed anymore, issue a whoami for example.

Now to your code:

who | sort -t: -k1 | tee All |cat;

The tee writes stdin to stdout on the screen and to the file "All". The content is also piped over to cat , which is not needed.

echo Total;

This is just echoed to the screen, not into the file.

cat All |wc -l;

Your temporary file entries are counted, but only written to screen, not to a file, so they are lost too.

rm All > Allusers

You delete your temporary file and redirect stdin to the file. As this rm will work without producing any output to stdout, you write nothing into the file "Allusers" and sowith empty it. Though, there was never yet in this line of code being written anything into this file yet, so ironically you couldn't overwrite anything anyway.

Thank you MAN, :slight_smile:

what a greate explanation :b:

I have modified it and it is working well

 who| cut -d " " -f1| sort > Allusers; echo "The total is: " >> Allusers; who| wc -l >> Allusers 

thank you again