Updates:

You can now choose from a number of pre-installed themes

Beginner bash script

Started by Finlay, December 01, 2015, 02:34:42 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Finlay

Hi folks  :) This is my first post, nice to meet y'all.
I'd really like some constructive criticism (hope I'm posting in the right place).


#! /bin/bash
for var in "[email protected]"
do
line=$(ps -fC $var | grep root)
if [ -z "$line" ]
then
killall -9 $var
fi
done
exit 0


Is the logic ok? Any potential problems?

Mark Greaves (PCNetSpec)

it's rubbish if it's supposed to show all logged on users....

Sorry, my roundabout way of saying "it might help if you stated what the script is supposed to do" ;)
WARNING: You are logged into reality as 'root'
logging in as 'insane' is the only safe option.
pcnetspec.co.uk


Finlay

 :) thanks.
Iterate over parameters, check if process is owned by root, if not kill it.

Mark Greaves (PCNetSpec)

#3
Wouldn't:
pgrep -v -u root
identify all non root owned processes by PID ?

as  the "-v" option makes it the "inverse" of:
pgrep -u root
then just pipe the returned PID list to "killall -9" .. just a thought.

or maybe I'm over-thinking it  :-\
WARNING: You are logged into reality as 'root'
logging in as 'insane' is the only safe option.
pcnetspec.co.uk

Mad Penguin

Mmm, I think all you need is;


#! /bin/bash
for var in "[email protected]"
do
  pkill -v -u root ${var}
done

But I'll let you test it on your system .. . :o
https://twitter.com/garethbult
https://gareth.bult.co.uk


Finlay

You guys are very funny!
That kills all processes except the one specified as the argument!

:)