我正在寻找在Mac OS x上复制Linux 'watch'命令的最佳方法。我想每隔几秒钟运行一个命令,使用'tail'和'sed'对输出文件的内容进行模式匹配。
我在Mac电脑上最好的选择是什么?不下载软件能做到吗?
我正在寻找在Mac OS x上复制Linux 'watch'命令的最佳方法。我想每隔几秒钟运行一个命令,使用'tail'和'sed'对输出文件的内容进行模式匹配。
我在Mac电脑上最好的选择是什么?不下载软件能做到吗?
当前回答
上面的shell可以做到这一点,你甚至可以将它们转换为别名(你可能需要包装一个函数来处理参数):
alias myWatch='_() { while :; do clear; $2; sleep $1; done }; _'
例子:
myWatch 1 ls ## Self-explanatory
myWatch 5 "ls -lF $HOME" ## Every 5 seconds, list out home directory; double-quotes around command to keep its arguments together
Homebrew也可以从http://procps.sourceforge.net/:上安装手表
brew install watch
其他回答
使用MacPorts:
$ sudo port install watch
试试这个:
#!/bin/bash
# usage: watch [-n integer] COMMAND
case $# in
0)
echo "Usage $0 [-n int] COMMAND"
;;
*)
sleep=2;
;;
esac
if [ "$1" == "-n" ]; then
sleep=$2
shift; shift
fi
while :;
do
clear;
echo "$(date) every ${sleep}s $@"; echo
$@;
sleep $sleep;
done
上面的shell可以做到这一点,你甚至可以将它们转换为别名(你可能需要包装一个函数来处理参数):
alias myWatch='_() { while :; do clear; $2; sleep $1; done }; _'
例子:
myWatch 1 ls ## Self-explanatory
myWatch 5 "ls -lF $HOME" ## Every 5 seconds, list out home directory; double-quotes around command to keep its arguments together
Homebrew也可以从http://procps.sourceforge.net/:上安装手表
brew install watch
或者,在你的~/。bashrc文件:(
function watch {
while :; do clear; date; echo; $@; sleep 2; done
}
当你的主命令需要相当长的时间才能完成时,为了防止闪烁,你可以捕获输出,并只在它完成时清除屏幕。
function watch {while :; do a=$($@); clear; echo "$(date)\n\n$a"; sleep 1; done}
然后用它:
watch istats