Author Topic: Beginner bash script  (Read 1973 times)

0 Members and 1 Guest are viewing this topic.

Offline Finlay

  • Jr. Member
  • **
  • Posts: 5
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Beginner bash script
« on: December 01, 2015, 02:34:42 pm »
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).

Code: [Select]
#! /bin/bash
for var in "$@"
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?

Offline Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 17351
  • Karma: 442
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: Beginner bash script
« Reply #1 on: December 01, 2015, 08:16:11 pm »
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

Offline Finlay

  • Jr. Member
  • **
  • Posts: 5
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Re: Beginner bash script
« Reply #2 on: December 02, 2015, 09:52:57 am »
 :) thanks.
Iterate over parameters, check if process is owned by root, if not kill it.

Offline Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 17351
  • Karma: 442
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: Beginner bash script
« Reply #3 on: December 02, 2015, 11:39:07 pm »
Wouldn't:
Code: [Select]
pgrep -v -u root
identify all non root owned processes by PID ?

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

or maybe I'm over-thinking it  :-\
« Last Edit: December 02, 2015, 11:42:05 pm by Mark Greaves (PCNetSpec) »
WARNING: You are logged into reality as 'root'
logging in as 'insane' is the only safe option.
pcnetspec.co.uk

Offline Mad Penguin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1419
  • Karma: 10018
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Beginner bash script
« Reply #4 on: December 03, 2015, 02:02:54 am »
Mmm, I think all you need is;

Code: [Select]
#! /bin/bash
for var in "$@"
do
  pkill -v -u root ${var}
done
But I'll let you test it on your system .. . :o

Offline Finlay

  • Jr. Member
  • **
  • Posts: 5
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Re: Beginner bash script
« Reply #5 on: December 03, 2015, 11:46:31 am »
You guys are very funny!
That kills all processes except the one specified as the argument!

 :)

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal