1

I'm trying to learn some more Linux, and from experience the best way is to try to bang your head against the wall. So now that I've done a task manually a few times I would like to automate it. This involves making a oneliner to kill some tasks so I can restart them.

At the moment I'm working with the following:

for i in `ps aux | egrep "[c]ouchpotato|[s]abnzbd|[s]ickbeard|[d]eluge|[n]zbhydra"|awk '{print $2, $11, $12}'`; do echo $i; done

The thing is that as soon as I run the for loop it breaks up the lines I get from awk.

Running ps aux | egrep "[c]ouchpotato|[s]abnzbd|[s]ickbeard|[d]eluge|[n]zbhydra"|awk '{print $2, $11, $12}' gives me the result I'm looking for, namely:

27491 /usr/local/couchpotatoserver-custom/env/bin/python /usr/local/couchpotatoserver-custom/var/CouchPotatoServer/CouchPotato.py
27504 /usr/local/deluge/env/bin/python /usr/local/deluge/env/bin/deluged
27525 /usr/local/deluge/env/bin/python /usr/local/deluge/env/bin/deluge-web
27637 /usr/local/nzbhydra/env/bin/python /usr/local/nzbhydra/share/nzbhydra/nzbhydra.py
27671 /usr/local/sabnzbd/env/bin/python /usr/local/sabnzbd/share/SABnzbd    /SABnzbd.py
28084 /usr/local/sickbeard-custom/env/bin/python /usr/local/sickbeard-custom/var/SickBeard/SickBeard.py

But adding it to my for loop breaks it into:

27491
/usr/local/couchpotatoserver-custom/env/bin/python
/usr/local/couchpotatoserver-custom/var/CouchPotatoServer/CouchPotato.py
27504
/usr/local/deluge/env/bin/python
/usr/local/deluge/env/bin/deluged
etc...

My goal is for $i to contain the whole line - is this possible? Also, is it possible to use get only the command from $11 and $12? I don't need to have the whole path to python and I don't even need to have the whole path to the application.

Thanks!

sourcejedi
  • 50,249
Lars
  • 13

1 Answers1

1

Notice that the for loop output is broken apart at the word boundaries, viz., whitespaces/newlines. Whereas what you said you wanted is the whole line to come contained in the $i.

So you need to do these 2 things:

  1. set the input field separator to a newline.
  2. disable the wildcards expansion.

    set -f;IFS=$'\n'; for i in `.....`;do echo "$i"; done
    

Note: DO NOT quote the backquotes else you shall end up giving to the for loop one big blob of argument,which would be the whole ps's output, and that doesn't do you no good.

HTH

  • Thanks! That fixed the main part of my problem :)

    P.S. I wasn't allowed to upvote your answer as I'm too new...

    – Lars Jul 02 '17 at 17:15