Solved

How to execute a command and save the results ??

Posted on 2004-05-03
8
397 Views
Last Modified: 2008-02-26
Simple:

I want to execute the command "ls" in my mandrake 10.

I have a variable "string command;", so I want that "command" gets the result of the "ls" command.

Something like this:

string command;  // Variable
command = // Whatever is under "ls"
cout << command; // This should print home  folders  etc...
0
Comment
Question by:digi_mind
8 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10978770
Hi digi_mind,
What you're looking for is the popen command:

#include <stdio.h>

FILE* ls_output = popen("ls", "r");
[...]
pclose(ls_output);

...but unfortunately, this uses C-style, buffered IO. I'm not sure if there is a constructor for an istream which takes a FILE* as argument. Try it out.

Cheers,
Stefan
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 25 total points
ID: 10978899
Try this:

#include <stdio.h>

main(){
    char buffer[1024];
    string command;
    size_t chars;

    FILE* ls_output = popen("ls", "r");
    while(chars = fread(buffer,1,sizeof(buffer),ls_output))
        command.append(buffer,chars);    

    pclose(ls_output);

    cout << command;

}
0
 

Author Comment

by:digi_mind
ID: 10984364
I have this to execute a specific command in Linux

#include <stdio.h>

main(){
    char buffer[1024];
    string command;
    size_t chars;

    FILE* ls_output = popen("ls", "r");
    while(chars = fread(buffer,1,sizeof(buffer),ls_output))
        command.append(buffer,chars);    

    pclose(ls_output);

    cout << command;

}

what can I do to execute any command not only the one I specify, for example I have in a string called request command, which contains any command that a user types, I want that command to be executed and the result displayed.
How can I do that ?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 25 total points
ID: 10984906
Cheap & cheerful solution:
Pass the command in quotes, e.g.:-
myprog "ls -A -Fx"

Now change your above program line 8 to be 'FILE* ls_output = popen(argv[1], "r");'
0
 

Author Comment

by:digi_mind
ID: 10984965
Ok, I will try to ask in a simplier way:

This is my program:
*******************
string command;
cin >> command;
FILE * test = popen(command , "r");
***********************************
This does not work... and I do not know why. Any suggestion ???

0
 
LVL 1

Expert Comment

by:arn0ld
ID: 10992114
how about a
 cout<<command<<endl;
after cin?

You are calling  MyProgram as
echo ls|MyProgram
not as
MyProgram ls  
?
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now