简体   繁体   中英

Show not logged users processes linux bash script

I am doing a bash script and i am essaying to show not logged users processes,which are typically daemon processes, for this,in the exercise, they recommend me:

To process the command line, we will use the cut command, which allows selecting the different columns of the list through a filter.

I used:

ps -A | grep -v w
ps -A | grep -v who 

ps -A | grep -v $USER

but trying all these options all the processes of all users are printed in the output file, and I only want the processes of users who are not logged.

I appreciate your help

Thank you.

grep -vw will remove lines matching the regular expression w (which is simply anything which contains the string w ). To run the command w you have to say so; but as hinted in the instructions, you will also need to use cut to post-process the output.

So as not to give the answer away completely, here's rough pseudocode.

w | cut something >tempfile
ps -A | grep -Fvf tempfile

It would be nice if you could pass the post-processed results of w in a pipe, but standard input is already tied to ps -A . If you have a shell which supports process substitution , you can use that.

ps -A | grep -Fvf <(w | cut something)

Unfortunately, the output from w is not properly machine-readable -- you will properly want to cut out the header line(s), too. (On my machine, there are two header lines. Yours might differ.) You'll probably learn a bit of Awk later on in the course, but until then, maybe something like

ps -A | grep -Fvf <(w | tail -n +3 | cut something)

This still doesn't completely handle all possible situations. What if someone's account name is grep ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM