首页 > 解决方案 > 如何添加详细选项来控制脚本输出?

问题描述

标签: linuxbashshell

解决方案


如果要使输出静音 ( stdout),请运行:

exec >/dev/null

如果您想使某些命令静音而不是其他命令,请保存 stdout 的副本并根据需要重定向:

exec 6>&1         # Save current stdout as file handle 6
exec >/dev/null   # Silence stdout
date              # This command is silenced
exec >&6          # Restore stdout to its original destination
echo Hi           # This command will display

示例脚本

#!/bin/bash

# Save current stdout as file handle 6 and then set for silence
exec 6>&1
exec >/dev/null

# Process options
while getopts "v" opt; do
 case "$opt" in
 v) exec >&6 ;;
 esac
done

# Generate output
echo "Information out"

推荐阅读