当我运行此脚本时,它不会watch在屏幕上显示输出,并且不会在 5 秒后超时。
§ cat script.sh
#!/bin/bash
timeout 5s watch -n 1 ps
Run Code Online (Sandbox Code Playgroud)
这是我运行脚本时在屏幕上看到的:
但是,直接在终端中运行脚本的内容会按预期工作:即,
§ timeout 5s watch -n 1 ps
Run Code Online (Sandbox Code Playgroud)
watch当从脚本调用时,究竟发生了什么阻止在屏幕上显示内容?
此外,在脚本中使用--foreground选项timeout使其按预期工作,但我不明白为什么。
如果重要,以下是这些工具的版本:
§ bash --version
bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.
§ watch -v
watch from procps-ng 3.3.12-dirty
§ timeout --version
timeout (GNU coreutils) 8.29
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or …Run Code Online (Sandbox Code Playgroud)