Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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
Linux Bash脚本编译并运行C++;程序 我编写了一个BASH脚本来编译和运行C++程序。这是我的剧本 #!/bin/bash PROG_NAME=$1 output=$(g++ $PROG_NAME) #redirect the error to a variable echo $output #show the error on stdout if [$output = ""] then ./a.out fi_Linux_Bash - Fatal编程技术网

Linux Bash脚本编译并运行C++;程序 我编写了一个BASH脚本来编译和运行C++程序。这是我的剧本 #!/bin/bash PROG_NAME=$1 output=$(g++ $PROG_NAME) #redirect the error to a variable echo $output #show the error on stdout if [$output = ""] then ./a.out fi

Linux Bash脚本编译并运行C++;程序 我编写了一个BASH脚本来编译和运行C++程序。这是我的剧本 #!/bin/bash PROG_NAME=$1 output=$(g++ $PROG_NAME) #redirect the error to a variable echo $output #show the error on stdout if [$output = ""] then ./a.out fi,linux,bash,Linux,Bash,如果程序编译失败,我不想运行a.out文件。为此,我创建了一个变量来存储编译时错误消息。但是这种方法似乎不起作用,因为输出没有被重定向到变量。还有别的办法吗 编辑 这是我的剧本 #!/bin/bash PROG_NAME=$1 g++ -Werror $PROG_NAME if [[ $? == 0 ]]; then ./a.out fi 按照惯例,程序会在上写入错误输出 语句output=$(g++$PROG\u NAME)将仅用STDOUT(出错时为空)填充变量$output 你有

如果程序编译失败,我不想运行
a.out
文件。为此,我创建了一个变量来存储编译时错误消息。但是这种方法似乎不起作用,因为输出没有被重定向到变量。还有别的办法吗

编辑 这是我的剧本

#!/bin/bash
PROG_NAME=$1
g++ -Werror $PROG_NAME
if [[ $? == 0 ]]; then
    ./a.out
fi

按照惯例,程序会在上写入错误输出

语句
output=$(g++$PROG\u NAME)
将仅用STDOUT(出错时为空)填充变量
$output

你有不同的选择

保留您的设计,您需要做的是在STDOUT上重定向STDERR。 简单地说:


如果
g++
失败,它将返回一个非零返回值,可使用Bash
If
命令检查该值:

output=$(g++ $PROG_NAME 2>&1)
if [[ $? != 0 ]]; then
    # There was an error, display the error in $output
    echo -e "Error:\n$output"
else
    # Compilation successfull
    ./a.out
fi


一个可能更好的解决方案(IMO)是学习如何使用makefiles,因为它也将允许更复杂的项目。

您可以通过以下方法检查此可执行文件是否存在:

if test -f "$FILE"; then
    echo "$FILE exist"
    ./$FILE
fi
脚本开头使用此代码从文件夹中删除可执行文件。run.sh的完整内容:

#!/bin/bash
FILE=a.out
if test -f "$FILE"; then
    echo "$FILE removed!"
    rm $FILE
fi
PROG_NAME=$1
echo $PROG_NAME
output=$(g++ $PROG_NAME)  #redirect the error to a variable
echo $output              #show the error on stdout
if test -f "$FILE"; then
    echo "$FILE exist"
    ./$FILE
fi
然后使用以下命令运行:

bash run.sh main.cpp

@AkashKarnatak:在您的示例中,您没有将错误重定向到变量,而是将g++的标准输出存储到变量中

也许最简单的方法是让
g++
指示它是否成功。这可以通过以下方式实现:

g++ $PROG_NAME && ./a.out

显示所有错误,因为G++在错误上设置非零退出代码,只有在没有错误时才执行结果文件。

“BASH脚本编译和运行C++程序”BTW:这就是使被设计为。使用手工编写的脚本进行编译根本不是一个好主意。因此,您只需开始使用为您想做的工作而设计的工具!您可以将stdout重定向到stderr并打印
$output
。即使程序无法运行,G++仍然返回“0”compile@AkashKarnatak实际上不是,只是我的比较错了。更新$?无论程序是否编译,始终返回0。看看这个@AkashKarnatak。重要的是在编译和检查之间不要有任何命令。在我的例子中,g++返回的是警告而不是错误消息,这就是$&返回0的原因。如果我使用
g++-Werror$PROG_NAME
bash脚本工作正常
g++ $PROG_NAME && ./a.out