简体   繁体   English

将C ++程序的输出发送到变量bash中

[英]sending output of a c++ program into a variable bash

I'm trying to write a system that grades a c++ code with pre-written examples that i have ready. 我正在尝试编写一个系统,该系统使用我已经准备好的预写示例对C ++代码进行评分。 It's a very simple c++ code like the following: 这是一个非常简单的c ++代码,如下所示:

#include <iostream>
using namespace std;
int main()
{
    int a;
    cin >> a;
    if (a > 100)
        cout << "Big";
    else
        cout << "Small";
    return 0;
}

So i want to test and grade this program with a bash, declare a score variable and echo it in the end. 所以我想用bash测试该程序并对其评分 ,声明一个score变量并在最后回显它。 Here's what i wrote(I've marked where i need help writing with double quotes) 这是我写的(我在需要标记双引号的地方写了帮助)

#!/bin/bash
g++ new.cpp -o new
test1=101
test2=78
score=0
if [ "Result of executing ./new with $test1 variable as input"=="Big" ]
then
(( score += 50 ))
fi
if [ "Result of executing ./new with $test2 variable as input"=="Small" ]
then
(( score += 50 ))
fi
echo $score

Also i'm still very new to bash so if you can tell me a simpler way to use bash for the examples (like loops) i'd love to hear it. 而且我对bash还是很陌生,所以如果您能告诉我一种更简单的方法来使用bash作为示例(例如循环),我很想听听。 Thanks! 谢谢!

If you want to execute new with the params and get its result, you should try something like this: 如果要使用参数执行new并获得其结果,则应尝试执行以下操作:

#!/bin/bash
g++ new.cpp -o new
test1=101
test2=78
score=0
if [ $(./new $test1) == "Big" ]; then
    (( score += 50 ))
fi
if [ $(./new $test2) == "Small" ]; then
    (( score += 50 ))
fi
echo $score

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM