linux - 如何添加详细选项来控制脚本输出?
问题描述
解决方案
如果要使输出静音 ( 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"
推荐阅读
- python - 在 VSCode 中删除 Jupyter 单元格上方的按钮行
- javascript - JavaScript过滤器没有过滤掉错误的比较
- flutter - Flutter Hive 在使用 ListView.builder 时检索框元素并获取密钥
- flutter - 在 Dart 中创建机械过滤器时遇到问题
- javascript - 从相对路径链接访问页面时,不会触发 JavaScript“加载”事件侦听器
- java - JUnit5:将对象上的每个方法调用转发到包装函数
- javascript - 使用弹出模式按钮创建 Javascript 模板文字
- virtual-machine - 保护软件代码和知识产权自由职业者和外包
- javascript - 使用 AJAX 和 CodeIgniter 3 检索和显示值的总和
- r - 以协变量为条件的 OLS 预测图