First of all you can easily get pids with pgrep command which does the same thing like your code and you can even use pkill which kills running processes by their names on similar basis. You just have to be very cautious about what pattern you are killing processes with.
Second thing is that you can even use loops within one line simply by separating different lines with semi-colons.