1

I'd like to monitor apps that have inotify instance in real-time with watch using something similar to:

watch "for foo in /proc/*/fd/*; do readlink -f $foo; done | grep inotify | sort | uniq -c | sort -nr"

I was hoping the output would appear similar to

for foo in /proc/*/fd/*; do readlink -f $foo; done | grep inotify | sort | uniq -c | sort -nr

but instead this fails with a message about readlink having a missing an operand. I've been using using quotes to wrap my complex shell commands to view in watch for a while now and I guess I was surprised this one didn't work. I guess watch is designed for something different, just running a command and updating the output it sees. But I can't seem to bring this command up in sh or bash with:

watch "/bin/bash for foo in /proc/*/fd/*; do readlink -f $foo; done | grep inotify | sort | uniq -c | sort -nr"
watch bash "for foo in /proc/*/fd/*; do readlink -f $foo; done | grep inotify | sort | uniq -c | sort -nr"
watch sh "for foo in /proc/*/fd/*\; do readlink -f $foo\; done | grep inotify | sort | uniq -c | sort -nr"
watch bash "for foo in /proc/*/fd/*\; do readlink -f $foo\; done | grep inotify | sort | uniq -c | sort -nr"

but this gives sh: 1: Syntax error: "do" unexpected, and/or various syntax errors. After trying this one last thing (escaping the ; with \;) but I get /bin/bash: for: No such file or directory. but am curious to know why this works and if I can avoid using a script file, eg: watch which-programs-created-inotify-instances.sh

roaima
  • 107,089
  • 14
  • 139
  • 261
Tomachi
  • 111
  • 6
  • 2
    Use single quotes or `$foo` will be evaluated by your current shell and not the shell started by `watch`. – muru May 03 '23 at 05:14
  • 2
    Related, if not a duplicate - [What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?](https://unix.stackexchange.com/q/503013/100397) – roaima May 03 '23 at 07:14
  • Hello, yes, it is better to answer your own question, and mark it as solved. Thank you for your feedback ! – ramius May 03 '23 at 12:50

0 Answers0