Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何在';如果';?_Bash_Shell_Unix_Zsh - Fatal编程技术网

Bash 如何在';如果';?

Bash 如何在';如果';?,bash,shell,unix,zsh,Bash,Shell,Unix,Zsh,我正在编写一个shell脚本,以便在gcc不返回错误或打印错误时运行。如何在不再次编译代码的情况下打印错误?嗯,最简单的方法是: if g++ test.cpp > /dev/null 2>&1 then ./a.out else g++ test.cpp fi 嗯,最简单的事情是: if g++ test.cpp > /dev/null 2>&1 then ./a.out else g++ test.cpp fi

我正在编写一个shell脚本,以便在gcc不返回错误或打印错误时运行。如何在不再次编译代码的情况下打印错误?

嗯,最简单的方法是:

if  g++ test.cpp > /dev/null 2>&1
then
    ./a.out
else
    g++ test.cpp
fi

嗯,最简单的事情是:

if  g++ test.cpp > /dev/null 2>&1
then
    ./a.out
else
    g++ test.cpp
fi

如果打印错误只是指stderr的输出,那么您可以这样做

if g++ test.cpp > /tmp/some_temp_file 2>&1
then
    ./a.out
else
    cat /tmp/some_temp_file
fi
rm /tmp/some_temp_file

但是,更一般地说,将输出重定向到文件(如depesz的回答中所述)是最简单的方法,它还为您提供了根据自己的目的处理输出的能力。任何其他伎俩都可能存在可移植性问题,但请记住,重定向可能因其自身原因而失败(文件不可写,磁盘上没有更多空间等)。

如果打印错误只是指stderr的输出,那么您可以这样做

if g++ test.cpp > /tmp/some_temp_file 2>&1
then
    ./a.out
else
    cat /tmp/some_temp_file
fi
rm /tmp/some_temp_file

但是,更一般地说,将输出重定向到文件(如depesz的回答中所述)是最简单的方法,它还为您提供了根据自己的目的处理输出的能力。任何其他伎俩都可能存在可移植性问题,但请记住,重定向可能因其自身的原因而失败(文件不可写,磁盘上没有更多空间等)。

请注意,bash具有
&>文件
重定向,该重定向将stdout和stderr发送到文件您只想看到错误消息吗,即“正常”编译器消息不存在?请注意,bash具有
&>文件
重定向,该重定向将stdout和stderr发送到该文件是否只查看错误消息,而不查看“正常”编译器消息?