【问题标题】:No colour output if program run with exec如果程序使用 exec 运行,则没有颜色输出
【发布时间】:2015-05-09 09:41:26
【问题描述】:

我有一个可以运行的 C 程序

execvp("grep", args);

其中 args 是一个数组 {"grep", "test"}(test 是我希望 grep 找到的单词)。 问题是 grep 的输出没有着色。在普通的 bash shell 中,grep 以红色突出显示 test,但在我的程序输出中,它只打印如下行 this is a test 没有任何突出显示或任何颜色。

我还使用 execvp 来执行一些其他命令,这些命令也会在 bash shell 中产生彩色输出,并且在我的输出中没有颜色。

有没有办法解决这个问题?我需要对stdout 做些什么?

【问题讨论】:

    标签: c linux stdout


    【解决方案1】:

    您需要为grep 提供正确的选项才能使其着色。您的 shell 环境很可能包括:

    alias grep='grep --color=auto'
    

    但是execvp 对别名一无所知。

    所以创建 args 数组:{"grep", "--color=auto", "test", 0} 并在您的 execvp("grep", args); 调用中使用它。

    【讨论】:

      猜你喜欢
      • 2015-11-05
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2023-03-29
      • 1970-01-01
      • 2023-02-04
      • 2018-09-02
      相关资源
      最近更新 更多