OS X 中的Bash,'watch'命令

19 浏览
0 Comments

OS X 中的Bash,'watch'命令

我正在寻找在Mac OS X上复制Linux \'watch\'命令的最佳方法。我想每隔几秒钟运行一次命令,使用\'tail\'和\'sed\'对输出文件的内容进行模式匹配。

在Mac上,我的最佳选择是什么,是否可以在不下载软件的情况下完成?

admin 更改状态以发布 2023年5月23日
0
0 Comments

您可以通过shell循环来模拟基本功能:

while :; do clear; your_command; sleep 2; done

这将无限循环,清除屏幕,运行您的命令,并等待两秒钟 - 这是基本的watch your_command实现。

您还可以进一步创建一个watch.sh脚本,可以接受your_commandsleep_duration作为参数:

#!/bin/bash
# usage: watch.sh  
while :; 
  do 
  clear
  date
  $1
  sleep $2
done

0
0 Comments

安装了 Homebrew 后:

brew install watch

0