This question already has answers here:
How do I execute a command and get the output of the command within C++ using POSIX?
(12 answers)
How to run a bash script from C++ program
(5 answers)
Closed 8 years ago.
How might I go about writing a program in C that executes a .sh (shell script) file? I'm writing a program, and one of the functions requires running some shell scripts. Thanks!
Use the system function
system("myscript.sh");
Related
This question already has answers here:
Batch string replace
(3 answers)
Closed 6 years ago.
i'm new to batch programming but i cant find how to manipulate string in a specific manner like we would do in C++.
i want to create a batch file which would change.
00:1E:90:75:9F:9F
to
001E90759F9F
maybe you can try this:
sed 's/00:1E:90:75:9F:9F/001E90759F9F/' your_file_name
This question already has answers here:
How can I run an external program from C and parse its output?
(8 answers)
Closed 7 years ago.
I want to save the output from a bash script which is invoked from a C program to a variable declared in the C program. I searched and tried successfully calling a script using system, and I tried this, but it didn't work:
char* a;
system("a=`ls`");
printf("%s",a);
Use popen() system call. You can pass the cmd as the parameter. You will get the command output as text when the function returns. Hope this helps.
This question already has answers here:
C++ system() function — How to collect the output of the issued command?
(8 answers)
Closed 8 years ago.
Showing snippet of c code.
char command[]="curl -X POST -d \'{\"device_id\": \"2204\"}' http://example.com/configure";
.
.
system(command);
.
.
Now this gives the output in terminal. I want to get this output in variable.. How should i acheive that ?
Pretty sure it's been answered here.
popen(3)
C++ system() function — How to collect the output of the issued command?
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*.
This question already has answers here:
Problem redirecting a C program output in bash
(5 answers)
Closed 9 years ago.
So this is probably a stupid question, but I can't see what I'm doing wrong.
I am running a program that produces output when called like ./ar. The output looks like:
-0.00781 0.02344 0.98828
-0.01172 0.02734 0.98828
-0.01562 0.02344 0.98047
-0.00781 0.02344 1.00000
-0.00391 0.02344 0.98438
A new line of output is written every second.
When I call the code like this ./ar > log and kill the program using ctrl-c after a few seconds, the file log is empty.
I am running this code on an embedded system. The system has a writeable partition which is the partition that I am running in, and I have write access as I am logged in as root.
The reason is the lazy writing concept of UNIX system.
Are you sure you are looking at standard output in you call ./ar? It might be standard error.
So, try ./ar >log 2>err to have 2 files, one for stdout and one for stderr.
Or use ./ar 2>&1 >log to get one file for both streams.