Hello Id like some help with a problem Ive been having Im pr

Hello! I\'d like some help with a problem I\'ve been having.

I\'m programming in C under a linux environment. I\'d like to know how to make a C program simulate typing \"help\" in the linux terminal.

So for example the command \"ls\" in linux. This is the code I used to display the result of \"ls\" within a C program:

#include <stdio.h>

#include <stdlib.h>

int main()

{

system(\"ls\");

return 0;

}

It\'s not the complete code, it\'s just a test code to see if I can even get the commands to work within the C program (I\'m completely new to both C and working in the linux environment by the way.) However, it\'s the code that actually executes the \"ls\" command within the C program. I really want to know how to make the \"help\" command be able to execute within the same C program. Applying this method to the \"help\" command gives me this result: \"sh: 1: help: not found\". I can\'t seem to figure this problem out.

Any help would be greatly appreciated!

Solution

#include <stdio.h>
#include <stdlib.h>

int main()

{
   // valid command in unix
   // gives the files in current folder
    system(\"ls\");

   // sh will look for executable in /bin and /usr/bin directory.
    // and sh does not contain help there. hence the command is not found.
    system(\"help\");

    // gives the path of HOME
    system(\"echo $HOME\");

    return 0;

}

Hello! I\'d like some help with a problem I\'ve been having. I\'m programming in C under a linux environment. I\'d like to know how to make a C program simulate

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site