今天在写程序的时候,碰到个bug,排查了两个小时才发现。
大概是这样的:
在程序中想把一条命令执行结果重定向到a.txt。
例如ps > a.txt。
发现a.txt内容一直为空。原来以前封装的system函数中使用了这个判断,
if (NULL == strstr(cmd, "echo"))
{
strncat(cmd, " > /dev/null", 12);
}
把所有命令结果都给重定向到/dev/null中去了,相当于执行了:
ps > a.txt > /dev/null。
这时a.txt内容是空的。