仅针对 bash 中的一行禁止 `set -v` / `set -x` 日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19014576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:14:36  来源:igfitidea点击:

Suppress `set -v` / `set -x` logging only for a single line in bash

bashecho

提问by Monster Hunter

I'm writing a .sh script like the following:

我正在编写一个 .sh 脚本,如下所示:

echo script started
./some_command > output_file
echo script ended

I'd like the output to be the following:

我希望输出如下:

script started   
./some_command > output_file   
script ended  

I've tried set -v, set +vbut it didn't work properly,
is there anything similar to @echoin batch?

我试过了set -vset +v但它不能正常工作,
有没有类似于@echo批处理的东西?

Thanks

谢谢

采纳答案by Gilles Quenot

This does what you'd like :

这做你想要的:

#!/bin/bash

echo "script started"
echo "./some_command > output_file"
./some_command > output_file
echo "script ended"

回答by beroe

Using bash -xfrom the comment by @TopGunCoder:

使用bash -x@TopGunCoder 的评论:

#!/usr/bin/env bash -x
echo 
+ echo /Users/myuser/scripts/tbash.sh started
+ ls
[...my directory listing...]
+ pwd
/Users/myuser
+ ./some_command
[the output of "some command"]
+ date
Wed Sep 25 17:06:25 PDT 2013
+ echo /Users/myuser/scripts/tbash.sh ended
started > /dev/null ls pwd ./some_command > output_file date echo ##代码## ended > /dev/null

The OUTPUT is each command followed by its result:

OUTPUT 是每个命令后跟其结果:

##代码##

回答by TopGunCoder

Instead of using echojust replace it with #that way echo isn't displayed and you still get some feedback

而不是使用echo只是用#这种方式替换它,回声不会显示,你仍然会得到一些反馈