如何使用 `watch` 使用 bash-specific 语法?
How do I use bash-specific syntax using `watch`?
我可以在 Linux 中成功 运行 这个 pyrg
命令:
python -m unittest discover |& pyrg
> ...
> ----------------------------------------------------------------------
> Ran 3 tests in 0.001s
>
> OK
但不是这个:
watch -n 1 --color 'python -m unittest discover |& pyrg'
> Each 1.0s python -m unittest discover |& pyrg
>
> sh: 1: Syntax error: "&" unexpected
我也找不到如何使用 pyrg
的另一种形式与 python -m
而不是实际脚本:
watch -n 1 --color pyrg python -m unittest discover
> Usage: pyrg [options] TEST_SCRIPT.py
> : python TEST_SCRIPT.py |& pyrg
>
> pyrg: error: no such option: -m
我该如何解决这个问题?
根据man bash
:
If |& is used, command's standard error, in addition to its standard output, is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |.
所以,这应该有效:
watch -n 1 --color 'python -m unittest discover 2>&1 | pyrg'
为了使用 bash 特定的语法,您需要确保 运行 您的命令带有 bash shell:
watch -n 1 --color 'bash -c "python -m unittest discover |& pyrg"'
实际上,您可以删除一级引用:
watch -n 1 --color --exec bash -c 'python -m unittest discover |& pyrg'
我可以在 Linux 中成功 运行 这个 pyrg
命令:
python -m unittest discover |& pyrg
> ...
> ----------------------------------------------------------------------
> Ran 3 tests in 0.001s
>
> OK
但不是这个:
watch -n 1 --color 'python -m unittest discover |& pyrg'
> Each 1.0s python -m unittest discover |& pyrg
>
> sh: 1: Syntax error: "&" unexpected
我也找不到如何使用 pyrg
的另一种形式与 python -m
而不是实际脚本:
watch -n 1 --color pyrg python -m unittest discover
> Usage: pyrg [options] TEST_SCRIPT.py
> : python TEST_SCRIPT.py |& pyrg
>
> pyrg: error: no such option: -m
我该如何解决这个问题?
根据man bash
:
If |& is used, command's standard error, in addition to its standard output, is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |.
所以,这应该有效:
watch -n 1 --color 'python -m unittest discover 2>&1 | pyrg'
为了使用 bash 特定的语法,您需要确保 运行 您的命令带有 bash shell:
watch -n 1 --color 'bash -c "python -m unittest discover |& pyrg"'
实际上,您可以删除一级引用:
watch -n 1 --color --exec bash -c 'python -m unittest discover |& pyrg'