Sample Header Ad - 728x90

Do I have to write a shell script purely to access bash for loops inside of say watch?

1 vote
0 answers
33 views
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
Asked by Tomachi (111 rep)
May 3, 2023, 01:44 AM
Last activity: May 3, 2023, 01:11 PM