我希望能够根据命令行开关将我脚本的某些输出重定向到/ dev / null.
我不知道该怎么做.
我不知道该怎么做.
以愚蠢的方式,它会是这样的(以一种过于简化的方式):
#!/bin/sh REDIRECT= if [ $# -ge 1 -a "$1" = "--verbose" ]; then echo "Verbose mode." REDIRECT='1>&2 > /dev/null' fi echo "Things I want to see regardless of my verbose switch." #... Other things... # This command and others along the script should only be seen if I am in verbose mode. ls -l $REDIRECT
请问有什么线索吗?
谢谢大家.
如果处于详细模式,请将STDOUT绑定到另一个句柄,否则将这些句柄链接到/ dev / null.然后编写脚本,以便可选的东西指向额外的句柄.
#!/bin/sh exec 6>/dev/null if [ $# -ge 1 -a "$1" = "--verbose" ]; then echo "Verbose mode." exec 6>&1 fi echo "Things I want to see regardless of my verbose switch." #... Other things... # This command and others along the script should only be seen if I am in verbose mode. ls -l >&6 2>&1
这应该让你开始.我不确定这是否是BASH特定的.这只是很久以前的记忆. 原文链接:https://www.f2er.com/bash/385384.html