2
for pid in $(ps -ef | grep "chrome" | awk '{print $2}'); do kill -9 $pid; done

Invalid read syntax: ")"

What's the right way to run this script in Eshell?

Nick
  • 4,423
  • 4
  • 24
  • 41
  • eshell for loop syntax: [link1](http://www.emacswiki.org/emacs/EshellForLoop), [link2](https://www.gnu.org/software/emacs/manual/html_node/eshell/for-loop.html) – Nsukami _ Dec 25 '14 at 09:45

1 Answers1

4

Ask Emacs:

  • C-h i to open info
  • g(eshell)for loop to open the node on for loops in eshell

or

  • M-x info-display-manual RET eshell RET
  • gfor loop

There it says:

Because Eshell commands can not (easily) be combined with lisp forms, Eshell provides a command-oriented `for'-loop for convenience. The syntax is as follows:

`for VAR in TOKENS { command invocation(s) }'

where TOKENS is a space-separated sequence of values of VAR for each iteration. This can even be the output of a command if TOKENS is replaced with `{ command invocation }'.

For your particular problem you have to remember that some commands have been replaced with eshell internals, including kill. If you can't kill a process with

for pid in { ps -ef | grep "chrome" | awk '{print $2}' } {kill $pid}

try this instead

for pid in { ps -ef | grep "chrome" | awk '{print $2}' } {/usr/bin/kill $pid}

Or possibly better:

for pid in { pgrep "chrome" } {/usr/bin/kill $pid}
  • I tried `for pid in { ps -ef | grep "chrome" | awk '{print $2}' } {kill $pid}`;unfortunately it doesn't work. – Nick Dec 25 '14 at 10:23
  • 1
    You need `grep "chrome" -` in the pipe here. See also http://www.masteringemacs.org/article/complete-guide-mastering-eshell for more eshell information. – Andrew Swann Dec 25 '14 at 12:34
  • @AndrewSwann I tried `grep "chrome" -` and it doesn't work. – Nick Dec 25 '14 at 14:31
  • When you say "unfortunately it doesn't work", what exactly do you mean? –  Dec 25 '14 at 16:43
  • @Nick: see my updated answer for something that ought to work. –  Dec 25 '14 at 16:50
  • On my system, kill is `/bin/kill`, both of your command works well. Thank you. (`pgrep` is really nice) – Nick Dec 26 '14 at 00:36