How to write a shell script to check the return value of main()

Just one example.

test_error.cpp containing the main function:

#include<iostream>
using namespace std;
int main()
{
	cout<<"This is just for an error simulation"<<endl;
	return 12;
}

Get an simple a.out, after compiling the above cpp file with g++ test_error.cpp

shell script(test_error.sh):

if ./a.out; then
    echo "This program runs successfully."
else
    echo "Error number is $?"
fi

In a shell script, 0 means true, while others are false.

Change its mode: chmod +x test_error.sh

Run the shell script: ./test_error.sh

And now you can find the output is 

This is just for an error simulation
Error number is 12

Don't forget ';' between 'a.out' and 'then' in test_error.sh, and '?' after '$' can catch the return value of a.out

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值