编译程序并仅在编译失败时报告(不显示错误)

问题描述:

$2具有C文件的路径。 问题是,当我编译有错误的文件时,它显示错误。我不想显示错误,我只是想说:“$ 2不能编译”。有任何想法吗?编译程序并仅在编译失败时报告(不显示错误)

cc $2 
if test ! $? = 0    
then 
     echo "$2 doesn't compile." 
     exit 1 # exit failure  
fi 

您可以通过它重定向到/dev/null抑制cc的输出:

if ! cc "$2" >/dev/null 2>&1 ; then 
     echo "$2 doesn't compile." 
     exit 1 # exit failure  
fi 
+0

谢谢。我会更多地考虑这一点。 –