LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pass the output of a shell script to c++ program (https://www.linuxquestions.org/questions/linux-newbie-8/pass-the-output-of-a-shell-script-to-c-program-4175473741/)

samasara 08-17-2013 02:42 PM

Pass the output of a shell script to c++ program
 
Hi dear users,

I have a shell script that produce 3 outputs. i want to pass these 3 outputs to a c++ program. I know that i should use system() function for running the shell script but i do not know how to send the output of my shell script to my c++ program?
for example iwant my c++ program to get 3 outputs(Acc,Get ,Third)and pass them to these 3 functions:
Quote:

fuzzy-->Setinput(1,Acc);
fuzzy-->Setinput(2,Get);
fuzzy-->Setinput(3,Third);
how would it be?
Thanks

TB0ne 08-17-2013 02:57 PM

Quote:

Originally Posted by samasara (Post 5011036)
Hi dear users,
I have a shell script that produce 3 outputs. i want to pass these 3 outputs to a c++ program. I know that i should use system() function for running the shell script but i do not know how to send the output of my shell script to my c++ program?
for example iwant my c++ program to get 3 outputs(Acc,Get ,Third)and pass them to these 3 functions:

If you know you should use the system function, why are you asking how to do it??

Since your related thread involved others writing a program for you, why don't you start this one by posting what you've done and tried on your own first, and tell us where you're stuck??

btmiller 08-17-2013 04:55 PM

Generally, pipes are used to pass the output of one program in as input to another program. A shell script is a program and most modern shells support pipes in various ways. When you say your script produces 3 different outputs, do you mean that it writes its output to three different file descriptors, or something else? As TB0ne indicates, some more detail about what you've tried would be very beneficial.

climaxnix 08-17-2013 05:24 PM

may be this helps:

first the code of the simple program:
Quote:

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {

cout << "argc:" << argc << endl;
for (int i =0; i< argc ; i++)
cout << "argv["<<i<<"]: " << argv[i] << endl;

return 0;
}
now the command line execution of it:
Quote:

$ ls -l
total 76
-rw-r--r-- 1 engineer engineer 1336 Aug 18 01:18 makefile
-rw-r--r-- 1 engineer engineer 231 Aug 18 01:09 objects.mk
-rwxr-xr-x 1 engineer engineer 58102 Aug 18 01:18 printArg
-rw-r--r-- 1 engineer engineer 528 Aug 18 01:18 sources.mk
drwxr-xr-x 2 engineer engineer 4096 Aug 18 01:18 src
$ ./printArg `ls`
argc:6
argv[0]: ./printArg
argv[1]: makefile
argv[2]: objects.mk
argv[3]: printArg
argv[4]: sources.mk
argv[5]: src

btmiller 08-17-2013 05:42 PM

OK, that helps a little bit, but what do you want the program to do differently? Should it print the contents of all the files passed to it as command line arguments, try to run them as commands, or something else?

samasara 08-18-2013 11:45 AM

Pass the output of a shell script to c++ program Reply to Thread
 
hi again dear users. my shell script is something like this:
Code:

#!/usr/bin/awk -f
BEGIN{Third=0}{
if ($1 == "GET") {
  Get=$2; if ( Get != "some kind of test , this is just example" )
      Get=9
  }
if ($1 == "Message:") {
    sub(/^Message:.+\[accuracy/,"",$0);
    gsub(/[^[:digit:]]/,"",$1);
    Acc=$1;
    printf "%d %d %d\n",Acc,Get,Third;
  }
}

as you can see the output of the shell script is 3 variables(this shell script extract 3 character from a file and assign values to them and the output of that is 3 variables(Acc,Get,Third).
i want to pass these three variables to my c++ code. i do not know exactly how would it be and how these 3 output can be send to a program that has something like this:
Quote:

fuzzy-->Setinput(1,Acc);
fuzzy-->Setinput(2,Get);
fuzzy-->Setinput(3,Third);
I consider the example but i could not use it for my sample. how would it be?

thanks for your kind and help.

Firerat 08-18-2013 02:15 PM

I think you need to read up on c++

Code:

#include <iostream>
using namespace std;
int main() {
int Acc, Get, Third;
while(cin >> Acc) {
    cin >> Get >> Third;
    cout<<"Orig line : "<<Acc<<" "<<Get<<" "<<Third<<"\n";
    Acc++;Get++;Third++;Third++;Third++;
    cout<<"new line  : "<<Acc<<" "<<Get<<" "<<Third<<"\n";
    };
}

Code:

/path/to/Script.awk /path/to/logs/*log | /path/to/Fuzzy

TB0ne 08-18-2013 02:37 PM

Quote:

Originally Posted by samasara (Post 5011429)
hi again dear users. my shell script is something like this:
Code:

#!/usr/bin/awk -f
BEGIN{Third=0}{
if ($1 == "GET") {
  Get=$2; if ( Get != "some kind of test , this is just example" )
      Get=9
  }
if ($1 == "Message:") {
    sub(/^Message:.+\[accuracy/,"",$0);
    gsub(/[^[:digit:]]/,"",$1);
    Acc=$1;
    printf "%d %d %d\n",Acc,Get,Third;
  }
}

as you can see the output of the shell script is 3 variables(this shell script extract 3 character from a file and assign values to them and the output of that is 3 variables(Acc,Get,Third).
i want to pass these three variables to my c++ code. i do not know exactly how would it be and how these 3 output can be send to a program that has something like this:

I consider the example but i could not use it for my sample. how would it be?

Again, we know what you WANT, so now you need to show us what you've DONE. Post your C++ code, and tell us where you're stuck. This is becoming like your other thread where folks wrote a program for you...but you need to show effort of your own.

The VERY FIRST HIT in Google for "how to pass command line arguments in C++" is:
http://www.learncpp.com/cpp-tutorial...ine-arguments/

...amazingly, it tells you how, and has examples.

Firerat 08-18-2013 02:47 PM

AND

I gave an example on how to open a file in c++ in your other thread.

Which was in fact the first c/c++ I had written

And remember that awk script is not doing anything useful , you need to adapt it

samasara 08-19-2013 04:07 AM

Pass the output of a shell script to c++ program
 
Quote:

Originally Posted by Firerat (Post 5011508)
AND

I gave an example on how to open a file in c++ in your other thread.

Which was in fact the first c/c++ I had written

And remember that awk script is not doing anything useful , you need to adapt it

Hi dear user,

I try to do the job with the suggested code and try to add such a code in my cpp program. I compiled my program by makefile and there was no error but when i try to do /path/to/Script.awk /path/to/logs/*log | /path/to/Fuzzy there is some errors and one of them is this:
Quote:

/root/Sample/example/fuzzycode2.cpp: line 15: using: command not found
. other errors related to some fault in whole codes. these are this:
Quote:

/root/Sample/example/fuzzycode2.cpp: line 16: syntax error near unexpected token `('
and
/root/Sample/example/fuzzycode2.cpp: line 16: ` int main(){'

The other thing is i know that maybe awk does not solved my problem completely. but the interesting about this for me is extracting the value from my log file and the other thing is that i can change the if-else part and assign each value to it that i want. So the output of this for me is interesting and the biggest problem of me now is passing the output of shell script to my c++ program. I search a lot to remove the errors and try to edit my code but it seems something wrong is happening. Do you know what is the exact problem?
thanks alot

climaxnix 08-19-2013 04:13 AM

if i am not mistaken you said that your project is about fuzzy logic.

the information you give is not sufficient i guess. it is too fuzzy for us. :)

it is better you give more information about your code and the command you give to run the program.

samasara 08-19-2013 04:24 AM

Pass the output of a shell script to c++ program Reply to Thread
 
My c++ code is a code that assess risk based on fuzzy logic. I used a fuzzylogic library and then write such a code. my code is like this:
Code:

#include <iostream>
#include <stdlib.h>
#include "../FuzzyRule.h"
#include "../FuzzyComposition.h"
#include "../Fuzzy.h"
#include "../FuzzyRuleConsequent.h"
#include "../FuzzyOutput.h"
#include "../FuzzyInput.h"
#include "../FuzzyIO.h"
#include "../FuzzySet.h"
#include "../FuzzyRuleAntecedent.h"

 using namespace std;
  int main(){

      int Acc, Get, Third;
      Fuzzy* fuzzy = new Fuzzy();


        // Step 2 - Creating FuzzyInputs
        FuzzyInput* Threat = new FuzzyInput(1);
        FuzzySet* lowThreat = new FuzzySet(0, 1.875, 1.875, 3.75);
        Threat->addFuzzySet(lowThreat); //add fuzzy set low to threat level
        FuzzySet* mediumThreat = new FuzzySet( 3.125, 5,  5, 6.875);
      // other fuzzyset will be made like this i do not write them here just for making the code shorten.
     
 FuzzyOutput* Risk = new FuzzyOutput(1);

        FuzzySet* lowRisk = new FuzzySet(0, 1.875, 1.875, 3.75);
        Risk->addFuzzySet(lowRisk);


        //Step 3:Creating fuzzy rules
        //for example: 1. If (Threatlevel is Low) and (vulnerability is Low) and (Asset_value is High) then (Risk is Low)

        FuzzyRuleAntecedent* ThreatlowThreatAndVulnerabilitylowVulnerability = new FuzzyRuleAntecedent();
        ThreatlowThreatAndVulnerabilitylowVulnerability->joinWithAND(lowThreat, lowVulnerability);

        FuzzyRuleAntecedent* AssethighAsset = new FuzzyRuleAntecedent();
        AssethighAsset->joinSingle(highAsset);

        FuzzyRuleAntecedent* ifThreatlowThreatAndVulnerabilitylowVulnerabilityAndAssethighAsset = new FuzzyRuleAntecedent();
        ifThreatlowThreatAndVulnerabilitylowVulnerabilityAndAssethighAsset->joinWithAND(ThreatlowThreatAndVulnerabilitylowVulnerability, AssethighAsset);

        FuzzyRuleConsequent* thenRisklowRisk = new FuzzyRuleConsequent();
        thenRisklowRisk->addOutput(lowRisk);

        FuzzyRule* fuzzyRule1 = new FuzzyRule(1, ifThreatlowThreatAndVulnerabilitylowVulnerabilityAndAssethighAsset, thenRisklowRisk);
        fuzzy->addFuzzyRule(fuzzyRule1);
   
     
        fuzzy->setInput(1,Acc);
        fuzzy->setInput(2,Get);
        fuzzy->setInput(3,Third);
 
        fuzzy->fuzzify();

        cout << "Threat:" << lowThreat->getPertinence() << ", " << mediumThreat->getPertinence() << ", " << highThreat->getPertinence() << endl;
        cout << "Vulnerability:" << lowVulnerability->getPertinence() << ", " << highVulnerability->getPertinence() << ", " << endl;
        cout << "Asset:" << lowAsset->getPertinence() << ", " << highAsset->getPertinence() << endl;

 float output1 = fuzzy->defuzzify(1);

        cout << "Risk:" << output1 << endl;

        return 0;

As you can see 3 variables Acc, Get ,Third should be feed from a shell script that is like this:
Quote:


#!/usr/bin/awk -f
BEGIN{Third=0}{
if ($1 == "GET") {
Get=$2; if ( Get != "some kind of test , this is just example" )
Get=9
}
if ($1 == "Message:") {
sub(/^Message:.+\[accuracy/,"",$0);
gsub(/[^[:digit:]]/,"",$1);
Acc=$1;
printf "%d %d %d\n",Acc,Get,Third;
}
}
? how would it be?
Thanks alot.

climaxnix 08-19-2013 10:30 AM

you said
Quote:

I compiled my program by makefile and there was no error
and that:
Quote:

but when i try to do /path/to/Script.awk /path/to/logs/*log | /path/to/Fuzzy there is some errors and one of them is this:
so assuming that there is no error in compiling but execution of the program are you sure that /path/to/Fuzzy (in "/path/to/Script.awk /path/to/logs/*log | /path/to/Fuzzy" command) points to the executable (i guess "fuzzycode2" in your case) but not the cpp file (i.e. "fuzzycode2.cpp")?

Firerat 08-19-2013 12:41 PM

@samasara

your c++ code does not have a single cin, your Acc, Get and Third are all null..
You need to learn c/c++ , I'm not going to do it for you

and regards the 'errors'

Quote:

Code:

/root/Sample/example/fuzzycode2.cpp: line 15: using: command not found
/root/Sample/example/fuzzycode2.cpp: line 16: syntax error near unexpected token `('
and
/root/Sample/example/fuzzycode2.cpp: line 16: ` int main(){'


It looks to me like you are running your fuzzycode2.cpp source file as if it were a shell script!

look, the code you posted..
Code:

sh FuzzyLogic.cpp
FuzzyLogic.cpp: line 13: using: command not found
FuzzyLogic.cpp: line 14: syntax error near unexpected token `('
FuzzyLogic.cpp: line 14: `  int main(){'

obviously the numbers 'changed' it must be a different version...

How did you ever get this far?

Firerat 08-19-2013 12:52 PM

don't know why i'm doing this

Code:

using namespace std;
int Acc, Get, Third;
while(cin >> Acc >> Get >> Third) {
}



All times are GMT -5. The time now is 02:58 AM.