How may I loop over a set of PIDs delimited by spaces? I think I remember the first step is like making each element its own row?
For my Firefox sessions they are quite a few PIDs:
$ pidof firefox
315534 315333 315196 315007 314885 314550 314493 311685 310664 307575 306443 297316 284759 284396 279546 274917 274885 274850 274729 274669 274666 271203 241078 235645 229985 156754 156565 152252 151123 149938 149548 149491 147870 137040 136986 135960 134823 113097 109238 106897 106790 106729 106488 106482 106478 106447 106414 106367 106335 106304 106261 106192 106114 93683 61965 58700 58596 54029 53959 53609 52555 52272 52268 52265 48999 48840 47771 47330 44585 31973 31240 15503 15420 15415 15413 15216 15205 15201 15074 15037 15013 14981 14942 14688 11875 11837 11804 11769 11735 11725 11686 10724 10660 10617 10213 9110 9073 9022 8888 8856 8789 8755 8692 8626 8279 8245 8212 8173 6605 6589 6555 6491 6468 6431 6421 6355 6329 6319 6188 6135 6100 5912 5896 5880 5874 5868 5857 5850 5844 5841 5839 5679 5629 5058
Maybe it would be good to put them into an array?
Ultimately, I'm looking to get the number of open file handles.
All posted solutions are valid so far so it's hard to check one as "the" answer.
edit: Let me propose another one. Although it consists of two steps.
(1) myArr=$(pidof firefox)
"make two codes from one code, magic devider, I invoke you!"
(2) for i in "${myArr[@]}"; do echo "${i}"; done
But does anyone have an explanation why it is possible in (1) to write myArr=( $(pidof firefox) )
just as well? And why it is possible in (2) to just write for i in "${myArr[@]}"; do echo "$i"; done
even just as well?
Thanks for your answers! You're great!
kill
? In that case,pkill
would be a better tool. – Kusalananda Oct 25 '22 at 19:02myArr=$(pidof firefox)
does not create an array but a single string. It's also unclear whether your question in at the end supersedes the earlier question. If so, it would make more sense to post it separately (unless it's a duplicate, which I think it may be). – Kusalananda Oct 26 '22 at 12:42ls -l /proc/5058/fd
gives me lots of rows. How can I count them easily? – von spotz Oct 26 '22 at 13:44