LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-17-2013, 02:42 PM   #1
samasara
Member
 
Registered: Aug 2013
Posts: 34

Rep: Reputation: Disabled
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
 
Old 08-17-2013, 02:57 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,076

Rep: Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377
Quote:
Originally Posted by samasara View Post
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??
 
Old 08-17-2013, 04:55 PM   #3
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
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.
 
Old 08-17-2013, 05:24 PM   #4
climaxnix
LQ Newbie
 
Registered: Nov 2004
Distribution: Debian GNU/Linux
Posts: 12

Rep: Reputation: 2
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
 
Old 08-17-2013, 05:42 PM   #5
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
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?
 
Old 08-18-2013, 11:45 AM   #6
samasara
Member
 
Registered: Aug 2013
Posts: 34

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-18-2013, 02:15 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
1 members found this post helpful.
Old 08-18-2013, 02:37 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,076

Rep: Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377
Quote:
Originally Posted by samasara View Post
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.
 
Old 08-18-2013, 02:47 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
Old 08-19-2013, 04:07 AM   #10
samasara
Member
 
Registered: Aug 2013
Posts: 34

Original Poster
Rep: Reputation: Disabled
Pass the output of a shell script to c++ program

Quote:
Originally Posted by Firerat View Post
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

Last edited by samasara; 08-19-2013 at 04:11 AM.
 
Old 08-19-2013, 04:13 AM   #11
climaxnix
LQ Newbie
 
Registered: Nov 2004
Distribution: Debian GNU/Linux
Posts: 12

Rep: Reputation: 2
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.
 
Old 08-19-2013, 04:24 AM   #12
samasara
Member
 
Registered: Aug 2013
Posts: 34

Original Poster
Rep: Reputation: Disabled
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.

Last edited by samasara; 08-19-2013 at 04:25 AM.
 
Old 08-19-2013, 10:30 AM   #13
climaxnix
LQ Newbie
 
Registered: Nov 2004
Distribution: Debian GNU/Linux
Posts: 12

Rep: Reputation: 2
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")?
 
1 members found this post helpful.
Old 08-19-2013, 12:41 PM   #14
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
@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?

Last edited by Firerat; 08-19-2013 at 12:44 PM.
 
Old 08-19-2013, 12:52 PM   #15
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
don't know why i'm doing this

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to pass input from c program to shell script? kalyanilinux Programming 13 02-01-2012 10:06 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
how to pass MySQL user/pass securely in shell script? digity Linux - Newbie 5 01-07-2010 05:48 AM
How to pass the output of one program to another program without backticks? BrianK Linux - General 4 11-11-2008 06:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration