how to use the bash commands in C? - c

I was actually searching for the way to execute the output of bash built-in commands through a C program. Specifically I want to get the output of no.of processes running in the system through a C program . I found the command , now I want to get that through a C code .
Can anyone help me out of this ?

Related

Difference bewteen using shell command "command time -v" and C function "getrusage"?

I want to know the Maximum resident set size of a the execution of a shell file (which is launching C program).
Is there any difference of result between those two solutions :
command time -v ./file.sh
calling getrusage() function in my C program
I tried both and it seems clearly similar to me ... but I would like approval :)

How do I run a C program several times and record the outputs?

so basically I have a C program which does a lot of computation based on an input .txt file and outputs a value. I want to run it 100 times and then work out the average, obviously this would be tedious to do individually.
So I've tried to research a bit about scripting etc and I've found things like this:
https://answers.yahoo.com/question/?qid=20091206100348AAaJPP8
Am I supposed to just do this in my command prompt? (I'm on Windows btw)
Thanks for any help :)
You're on Windows, so you can use a DOS batch script (.bat) to run your program N times using a loop (or N separate commands if that's easier for you). Use the >> symbol at the end of the command to append the output to a file. See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true for more info on this, and search google for dos bat file for help on getting started with writing batch scripts.
Try this:
Have the program append the data into the text or csv file concerned and then write another program where you can run the program for a defined number of times. Use the function system(). It accepts a string as argument and executes it in the CUI.
Hope that helps.

How to get a program written in C to execute a command in Terminal or Command Prompt? [duplicate]

This question already has answers here:
Execute program from within a C program
(6 answers)
How do I execute a Shell built-in command with a C function?
(3 answers)
Closed 8 years ago.
I would like to write a program in C that can shutdown a computer. In order for me to do this, I must execute the command "shutdown -h now". This is to be done in the terminal or command prompt. How can I tell the program I would like it to execute the command in the command line?
There is a system-function, which basically does the c equivalent of the assembly systemcall:
It is located in
system("cls");
will clear the command screen for example.
To run a command in simple covenient way from C, use system :
system( "date");
That will execute the command under POSIX OSes, with the shell using your standard environment, so PATH is respected. Unfortunately, "shutdown -h now" requires privileges, so you may need to use sudo and enter a password.
You've got a bunch of functions which can do what you want:
system("shutdown -h now");
the exec* family : execlp("/sbin/shutdown", "shutdown", "-h", "now");
popen("shutdown -h now");
They all have advantages and drawbacks.
For example popen will return a FILE* which contains the output of the executed command.
Exec* will simply replace the memory area with your program by the one of the executed binary, this way if you want to continue the execution of your program you will have to use fork(2).
System is simply a helper function using fork and exec*.

Executing command through a c program on Windows

I want to know how to execute a command from a c program,on windows os.
To be more specific how to write a c program whose output will not be printed but directly goes to command prompt and get executed there? please help me
I think you need to use system() command in your C code.
For example:
system("pause");
where "pause" is the command to be executed in cmd.
reference: http://www.cplusplus.com/reference/cstdlib/system/
I hope i got your question right.
I'm not sure I understand the question correctly. But if I do, you're looking for the system() function.
I suspect what you are describing is the the back-ticks in shells in Linux/Unix.
However, I don't know how to do that in Windows.
Unix way
myprompt> `./a.out`
If the C program was basically: printf("ls -l .\n");, then this should list the files.
Is that what you wanted?
Like I said, I don't know how to do that in the Win Cmd Prompt, but maybe this clarifies your question.
Looks like you could try:
C:\MyDir> MyProgram.exe | cmd.exe /C

using exec to sort a text file in c

I have a text file full of records (output.txt) and I want to sort every record by its id. After the sorting the sorted records are written into a new file (sorted.txt).
To do that I am using bash's command "sort" via an execl() function. To check the validity of my sort command, I wrote the same command straight into the bash and the result is the expected one. But when I try to use the execl command through my C program, most of the time the answer will be that there is not a file /usr/bin/sort (I am using Mac OSX) or no error message will be thrown but nevertheless nothing happens...
What I am using is this:
execl("/usr/bin/sort", "usr/bin/sort", "-n","-k", "1", "-u", "output.txt", ">", "sorted.txt", (char*)NULL);
or this
execl("/usr/bin/sort", "usr/bin/sort", "-n","-k", "1", "-u", "-o", "sorted.txt", "output.txt", (char*)NULL);
I know that both of these 2 sort commands are correct when I m using them in the bash. What happens to C?
Thnx all in advance!
Output redirection (> somefile.txt) is a feature of the shell, not the sort program (which AFAIK is not a bash built-in).
The exec family of functions doesn't start the shell, only the program you've specified.
If you don't know the path to the program, you can use the functions with p in their names (execlp in your case, I think) and just give them "sort" as program name, they'll search for it in $PATH like bash does.
Alternatively you can try system("sort output.txt > sorted.txt"). The system function's behaviour is implementation dependent though on linux it basically spawns a new shell which executes the command passed to it. system(ARG) is equivalent to sh -c ARG. The redirection will work if the shell supports it in your system's implementation of the system function.

Resources