pipe as Bash function input
I maintain a system that deals with hundreds of text files at a time and find myself using infinite loops at the command line fairly frequently for doing things like monitoring a directory for a change, or check the number of files in a location every 5 seconds.
Usually this entails using something similar to Code:
while [ 1 ]; do clear; date; ls -al stuff/ | grep -v drwx | cut -d " " -f 6- ; sleep 5; done Code:
while [ 1 ]; do clear; date; ls -al stuff/ | wc -l; sleep 5; done Code:
loop () For example: This works Code:
loop ls -al stuff/ Code:
loop "ls -al stuff/ | wc -l" Hopefully someone can just tell me there's an easier way to do this. Thanks in advance, eldorel |
Try using eval $@ instead of echo `$@`.
ta0kira |
Just use the "watch" program. It is more flexible, made and tested especially to do this kind of things (see: man watch).
It comes as part of the procps package together with stuff like "top" and "vmstat", so I suppose all but the most stripped down distro's wil have it installed by default. To do your thing with "watch": Code:
watch "ls -al /tmp | wc -l" |
The problem is that when you run simply
Code:
loop ls -al stuff/ | wc -l However, if you have a command capable of taking buffered input on the right hand side of the pipe, it works. An example is grep Code:
loop ls -al stuff/ | grep -v drwx Code:
$var Code:
$@ Code:
loop () Hko, good advice! I always forget the watch command. |
Thank you
Eval... Duh.
See, I knew you guys would be able to pinpoint my mistake. Guess that's what I get for trying to fix a problem before I get my coffee, Thanks. As for reinventing the wheel, watch is exactly what I was looking for. Thanks for all of your help guys.. eldorel |
All times are GMT -5. The time now is 08:24 AM. |