[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to execute a command and save the results ??

Posted on 2004-05-03
8
Medium Priority
?
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 100 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

656 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