Solved

How to execute a command and save the results ??

Posted on 2004-05-03
8
427 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why VNC doesn't work in Redhat? 11 61
Linux VM 6 88
cannot rename datastore 3 45
Upgrade BIOS / EUFI at Scale 4 32
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

786 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