OSX bash,“观察”命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9574089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:44:08  来源:igfitidea点击:

OSX bash, 'watch' command

macosbashautomationwatch

提问by joseph.hainline

I'm looking for the best way to duplicate the Linux 'watch' command on Mac OSX. I'd like to run a command every few seconds to pattern match on the contents of an output file using 'tail' and 'sed'.

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

What's my best option on a Mac, and can it be done without downloading software?

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

回答by seantomburke

With Homebrewinstalled:

随着家酿安装:

brew install watch

brew install watch

回答by Daniel Pittman

You can emulate the basic functionality with the shell loop:

您可以使用 shell 循环模拟基本功能:

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

That will loop forever, clear the screen, run your command, and wait two seconds - the basic watch your_commandimplementation.

这将永远循环,清除屏幕,运行您的命令,然后等待两秒钟 - 基本watch your_command实现。

You can take this a step further and create a watch.shscript that can accept your_commandand sleep_durationas parameters:

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

#!/bin/bash
# usage: watch.sh <your_command> <sleep_duration>

while :; 
  do 
  clear
  date
  
  sleep 
done

回答by GoTLiuM

Use macports:

使用macports

$ sudo port install watch

回答by IT Gumby

the shells above will do the trick, you could even convert them to an alias (may need to wrap in a function to handle parameters)

上面的外壳可以解决问题,您甚至可以将它们转换为别名(可能需要包装在函数中以处理参数)

alias myWatch='_() { while :; do clear; ; sleep ; done }; _'

Examples:

例子:

myWatch 1 ls ## self-explanatory
myWatch 5 "ls -lF $HOME" ## every 5 seconds, list out home dir; double-quotes around command to keep its args together

Alternately, homebrew can install the watch from http://procps.sourceforge.net/

或者,自制软件可以从http://procps.sourceforge.net/安装手表

brew install watch

回答by ghoti

It may be that "watch" is not what you want. You probably want to ask for help in solving your problem, not in implementing your solution! :)

可能“观看”不是您想要的。您可能想寻求帮助来解决您的问题,而不是实施您的解决方案!:)

If your real goal is to trigger actions based on what's seen from the tailcommand, then you can do that as part of the tail itself. Instead of running "periodically", which is what watchdoes, you can run your code on demand.

如果您的真正目标是根据从tailcommand看到的内容触发操作,那么您可以将其作为 tail 本身的一部分来执行。watch您可以按需运行代码而不是“定期”运行。

#!/bin/sh

tail -F /var/log/somelogfile | while read line; do
  if echo "$line" | grep -q '[Ss]ome.regex'; then
    # do your stuff
  fi
done

Note that tail -Fwill continue to follow a log file even if it gets rotated by newsyslog or logrotate. You want to use this instead of the lower-case tail -f. Check man tailfor details.

请注意,tail -F即使它被 newsyslog 或 logrotate 轮换,它也会继续跟踪日志文件。您想使用 this 而不是小写的tail -f。检查man tail详细信息。

That said, if you really do want to run a command periodically, the other answers provided can be turned into a short shell script:

也就是说,如果你真的想定期运行一个命令,提供的其他答案可以变成一个简短的 shell 脚本:

#!/bin/sh
if [ -z "" ]; then
  echo "Usage: 
bash -c 'while [ 0 ]; do <your command>; sleep 5; done'
SECONDS COMMAND" >&2 exit 1 fi SECONDS= shift 1 while sleep $SECONDS; do clear $* done

回答by Marvin Pinto

Going with the answer from here:

这里开始回答:

brew install watch

But you're really better off installing watchas this isn't very clean..

但是你最好安装手表,因为这不是很干净..

回答by A_funs

If watch doesn't want to install via

如果手表不想通过安装

brew install visionmedia-watch

There is another similar/copy version that installed and worked perfectly for me

还有另一个类似/复制版本,它为我安装并完美地工作

function watch {
    while :; do clear; date; echo; $@; sleep 2; done
}

https://github.com/tj/watch

https://github.com/tj/watch

回答by jon

Or, in your ~/.bashrc

或者,在你的 ~/.bashrc

#!/bin/bash

function show_help()
{
  echo ""
  echo "usage: watch [sleep duration in seconds] [command]"
  echo ""
  echo "e.g. To cat a file every second, run the following"
  echo ""
  echo "     watch 1 cat /tmp/it.txt" 
  exit;
}

function show_help_if_required()
{
  if [ "" == "help" ]
  then
      show_help
  fi
  if [ -z "" ]
    then
      show_help
  fi
}

function require_numeric_value()
{
  REG_EX='^[0-9]+$'
  if ! [[  =~ $REG_EX ]] ; then
    show_help
  fi
}

show_help_if_required 
require_numeric_value 

DURATION=
shift

while :; do 
  clear
  echo "Updating every $DURATION seconds. Last updated $(date)"
  bash -c "$*"
  sleep $DURATION
done

回答by sugavaneshb

I had a similar problem.

我有一个类似的问题。

When I googled, I came across this link recently. This is not exactly 'installing software', simply getting the binary for 'watch' command.

当我用谷歌搜索时,我最近发现了这个链接。这不完全是“安装软件”,只是获取“监视”命令的二进制文件。

And the link is Get watch command for OSX

链接是获取 OSX 的监视命令

回答by Brad Parks

Here's a slightly changed version of this answerthat:

是这个答案的一个略有变化的版本:

  • checks for valid args
  • shows a date and duration title at the top
  • moves the "duration" argument to be the 1st argument, so complex commands can be easily passed as the remaining arguments.
  • 检查有效参数
  • 在顶部显示日期和持续时间标题
  • 将“duration”参数移动到第一个参数,因此可以轻松地将复杂命令作为剩余参数传递。

To use it:

要使用它:

  • Save this to ~/bin/watch
  • execute chmod 700 ~/bin/watchin a terminal to make it executable.
  • try it by running watch 1 echo "hi there"
  • 将此保存到 ~/bin/watch
  • chmod 700 ~/bin/watch在终端中执行以使其可执行。
  • 运行试试 watch 1 echo "hi there"

~/bin/watch

〜/斌/手表

##代码##