4
A
回答
10
嘗試
system("./helloworld.sh");
3
嘗試system()。
1
在C中還有execxxx
functions從unistd.h
。與簡單的system
相比,它們有很大的優勢,因爲您可以指定要在進程參數管理的其他控制級別中運行的進程的環境變量。
0
,如果你也想拿到劇本的輸出做
char fbuf[256];
char ret[2555];
FILE *fh;
if ((fh = popen("./helloworld.sh", "r")) == NULL) {
return 0;
}else{
while (fgets(fbuf, sizeof(fbuf), fh)) {
strcat(ret, fbuf);
}
}
pclose(fh);
5
如果你只是想如果你需要獲得標準輸入來運行它(沒有別的)
system("./helloworld.sh");
/stdout那麼你需要使用popen()
FILE* f = popen("./helloworld.sh","r");
1
至少有兩種可能的方法。 (我想你在問使用shell腳本的類Unix系統)。
第一個是非常簡單的,但是阻塞(它返回的命令已經完成後):
/* Example in pure C++ */
#include <cstdlib>
int ret = std::system("/home/<user>/helloworld.sh");
/* Example in C/C++ */
#include <stdlib.h>
int ret = system("/home/<user>/helloworld.sh");
的第二種方式是不容易,但可能是無阻塞(腳本可以作爲運行並行處理):
/* Example in C/C++ */
#include <unistd.h>
pid_t fork(void);
int execv(const char *path, char *const argv[]);
/* You have to fork process first. Search for it, if you don't know how to do it.
* In child process you have to execute shell (eg. /bin/sh) with one of these
* exec* functions and you have to pass path-to-your-script as the argument.
* If you want to get script output (stdout) on-the-fly, you can do that with
* pipes. Just create the reading pipe in parent process before forking
* the process and redirect stdout to the writing pipe in the child process.
* Then you can just use read() function to read the output whenever you want.
*/
+0
由於OP要求採用C++方式,所以不應該提及像fork/execv ...和btw這樣的東西,當您已經使用fork時,沒有理由使用execv而不是execv,還是存在? – smerlin 2010-07-07 22:51:27
相關問題
- 1. 如何在C中執行shell命令?
- 2. C#運行命令不執行命令
- 3. 如何使用c#執行AT命令#
- 4. 如何在iPhone中執行命令行?
- 5. 在c#中使用DbCommand執行命令#
- 6. 執行Windows命令用C
- 7. 執行dir命令在C:\
- 8. 如何從C++程序執行命令行命令
- 9. 如何可以從命令行在Linux中執行nagios命令
- 10. 如何從命令行在iTerm窗口中執行命令?
- 11. 在C++中調用命令行程序
- 12. 如何在c#中執行超過1行的sql命令
- 13. 如何在C++中並行執行系統命令
- 14. 如何:在C#中執行命令行,獲取STD OUT結果
- 15. 如何在vb6中調用命令行
- 16. 使用C執行命令行#
- 17. 在C#上執行PowerShell命令行
- 18. 使用C#程序如何執行命令提示符命令
- 19. 如何從c#執行cmd命令#
- 20. 如何從C#執行命令?
- 21. 如何調用命令行
- 22. 在Windows 8中執行cmd命令c#?
- 23. 在c程序中執行SET命令
- 24. C++在shell中執行許多命令
- 25. 如何在android中執行shell命令?
- 26. 如何在python中執行shell命令?
- 27. 如何在Kotlin中執行bash命令
- 28. 如何在Java中執行Windows命令?
- 29. 如何在java中執行cmd命令?
- 30. 如何在Django中執行Linux命令?
這是在「」頭或「」如果你喜歡這更好的。 –
mooware
2010-07-07 21:34:05
當你包含''時,它應該是'std :: system' - 參見http://stackoverflow.com/questions/2900785/ –
MSalters
2010-07-08 13:46:14