commit 25a65c6cfdd80d3be896e281ce66a899eb6da214 Author: marcelb Date: Tue Jun 27 10:38:27 2023 +0200 After make lib, and testing diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d8cb326 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "string": "cpp" + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6cef392 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Library for executing bash commands with captured output. + + +The output of the bash command is saved in a C++ string. \ No newline at end of file diff --git a/lib/exec.hpp b/lib/exec.hpp new file mode 100644 index 0000000..1be6b58 --- /dev/null +++ b/lib/exec.hpp @@ -0,0 +1,15 @@ +#ifndef _TRM_ +#define _TRM_ + +#include +// #include +// #include +// #include +#include + +using namespace std; + +string exec(const string command); + + +#endif \ No newline at end of file diff --git a/src/exec.cpp b/src/exec.cpp new file mode 100644 index 0000000..7203d18 --- /dev/null +++ b/src/exec.cpp @@ -0,0 +1,21 @@ +#include "../lib/exec.hpp" + +/** + * Funkcija za pozivanje bash naredbi s odgovorom +*/ + +string exec(const string command) { + FILE *fds; + char cstr[200]; + fds = popen(command.c_str(), "r"); + if (fds == NULL) { + throw string("[ERROR] Cant pipe stream in execute command "); + } + + string cppstr; + while (fgets(cstr, sizeof(cstr), fds) != NULL) { + cppstr += cstr; + } + pclose(fds); + return cppstr; +} diff --git a/test/bash_cmd.sh b/test/bash_cmd.sh new file mode 100644 index 0000000..69a97f4 --- /dev/null +++ b/test/bash_cmd.sh @@ -0,0 +1,3 @@ +echo "WARNING: key file (/etc/bind/rndc.key) exists, but using default configuration file (/etc/bind/rndc.conf)"; +sleep 4s; +echo "zone reload up-to-date"; \ No newline at end of file diff --git a/test/compile.sh b/test/compile.sh new file mode 100644 index 0000000..9a28838 --- /dev/null +++ b/test/compile.sh @@ -0,0 +1 @@ +g++ test.cpp ../src/* -o test.o \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..d7aea88 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,15 @@ +#include + +#include "../lib/exec.hpp" + +using namespace std; + +int main() { + + string out = exec("bash bash_cmd.sh"); + + cout << out << endl; + + + return 0; +} \ No newline at end of file diff --git a/test/test.o b/test/test.o new file mode 100755 index 0000000..11fb42f Binary files /dev/null and b/test/test.o differ