Capture entire output of bash commands while in ruby? - c++

I have a Ruby script that I have developed that allows me to install and build multiple C++ packages. I am able to execute the Ruby script and install the packages with no errors.
However, I would like to be able to capture all of the output, including cerr, to a "log" file of my choosing. I am able to redirect Ruby's cerr and standard output, but I cannot capture the bash commands: qmake, make, or make install cerr. The output still flows to the terminal.
I want to be able to run the Ruby script and not see any debug messages from any qmake, make, or make install bash commands, but be able to check a log file later for build results.

you can do
require 'open3'
log = File.new("#{your_log_dir}/script.log", "w+")
command = "make"
Open3.popen3(command) do |stdin, stdout, stderr|
log.puts "[OUTPUT]:\n#{stdout.read}\n"
unless (err = stderr.read).empty? then
log.puts "[ERROR]:\n#{err}\n"
end
end

%x[#insert bash command here] captures the response. If you need to handle STDERR you'll want to pipe it to STDOUT I believe.

To directly dump stdout and stderr of a child process to files:
cmd = ['ls', '-ahl', '/my/directory'] # example directory listing
pid = Process.spawn *cmd, out: '/path/to/stdout/log', err: '/path/to/stderr/log'
Process.wait pid
You may also pass file descriptors instead of path strings.

If you're on Mac OS or Linux, you can use standard redirection and a simple shell call if you want to capture the STDOUT and STDERR to a variable in your script:
asdf = `ls foo 2>&1`
asdf # => "ls: foo: No such file or directory\n"
2>&1 simply redirects STDERR in the command output to STDOUT, which is captured when the program runs.
If you want to redirect both STDOUT and STDERR to the same file for later, use:
`ls foo > output.log 2>&1`
The STDOUT has to be redirected before &2>1 will take effect, but that will capture both.
For more information see the "Redirect" section of man sh.

Related

Calling multiple commands via system() under windows doesn't work

So basiclly im trying to use this:
int main()
{
system("adb kill-server \n"
"adb devices \n"
"adb start-server & \n"
"var=$(adb shell \"pidof com.rok\")\n"
"AFTER=`echo $var | sed 's/\\r//g'`\n"
"echo \"$AFTER\"\n"
"adb shell \"kill -STOP $AFTER\"\n"
"adb shell sleep 2\n"
"adb shell \"kill -CONT $AFTER\"");
return 0;
}
thing is this works in Clion without any error, but i must do this in visual studio and in visual studio i cannot do it like that i have to do every system command alone like:
system("adb kill-server");
system("adb devices");
system("adb start-server");
system("var=$(adb shell \"pidof com.rok\")");
system("AFTER=`echo $var | sed 's/\\r//g'`");
system("adb shell \"kill -STOP $AFTER\"");
so now the thing is when i run it like this everything works except this two lines:
system("var=$(adb shell \"pidof com.rok\")");
system("AFTER=`echo $var | sed 's/\\r//g'`");
even though they perfectly works on clion they dont in visual studio, i cannot find a way to solve this problem, this is the error:
'var' is not recognized as an internal or external command,
operable program or batch file.
'AFTER' is not recognized as an internal or external command,
operable program or batch file.
can anyone explain why this happens? and how can i solve this problem?
Each call of system() creates it's own shell as a subprocess, that's why multiple subsequent system() calls don't work if you e.g. try to set shell variables or do a cd command (expecting subsequent commands running in a specific directory).
The easiest way to do it, is to create a little temporary script file containing all the commands and execute this one with a single system() call:
// A raw string literal to make escaping easier
// (see https://en.cppreference.com/w/cpp/language/string_literal)
std::string cmds = R"xxx(
adb kill-server
adb devices
adb start-server &
var=$(adb shell "pidof com.rok")
AFTER=`echo $var | sed 's/\r//g'`
echo "$AFTER"
adb shell "kill -STOP $AFTER"
adb shell sleep 2
adb shell "kill -CONT $AFTER"
)xxx";
std::ofstream cmdFile("tempCmdFile.cmd");
cmdFile << cmds;
cmdFile.close();
system("tempCmdFile.cmd");
You probably should tidy up the tempCmdFile.cmd up afterwards (i.e. remove it). But I hope you grasp what the code above does.
Also I am not so sure that
AFTER=`echo $var | sed 's/\r//g'`
will work in a windows shell, as you expect it to do. You probably need a different solution for that, or a *nix like shell to run it (e.g. MinGw provides one).

Hiding console command used while using system()? C++ Linux

Well, I will put it plain and simple: I am a C++ pleb. Still trying to learn though.
My question is: is it possible to run a command though the terminal using system() command without letting the command be shown in the console/terminal?
Example:
system("sudo service sshd start") ;
Output: Sudo service sshd start
Where as I want:
system("sudo service sshd start") ;
output: (Blank)
Note: I am on linux.
The system standard library function starts up a subshell and executes the provided string as a command within that subshell. (Note that a subshell is simply a process running a shell interpreter; the particular shell interpreter invoked by system will depend on your system. No terminal emulator is used; at least, not on Unix or Unix-like systems.)
The system function does not reassign any file descriptors before starting the subshell, so the command executes with the current standard input, output and error assignments. Many commands will output to stdout and/or stderr, and those outputs will not be supressed by system.
If you want the command to execute silently, then you can redirect stdout and/or stderr in the command itself:
system("sudo service sshd start >>/dev/null 2>>/dev/null") ;
Of course, that will hide any error messages which might result from the command failing, so you should check the return value of system and provide your own error message (or log the information) if it is not 0.
This really has very little to do with the system call or the fact that you are triggering the subshell from within your own executable. The same command would have the same behaviour if typed directly into a shell.

how to popen xterm on osx from c++ application?

I do this:
popen("xterm -e ' some bash script ' ","r");
and it works fine if I launch my application from a terminal command line.
but if I double click in the finder to launch it (i.e. non terminal), the application runs, but the xterm doesn't appear.
(xterm is maybe not the right solution on osx, what I want to do is to open a terminal from popen, interact with the user inside the terminal, and return the result of this interaction to the main program)
The output shown in xterm's window (or likely other terminals) will not be read by popen, so that part is unclear. However, you say that works from a terminal window.
Another problem is that the DISPLAY variable needed to run xterm may not be set in the environment where the finder is running. You can work around that by adding a suitable -display option to the command-line. For instance, if your application is running and displaying on the local machine (likely), you could try
popen("xterm -display :0.0 -e ' some bash script ' ","r");
When capturing output from xterm, there are two types of output to consider:
error messages from xterm itself are written to the standard error
the program running inside xterm, e.g., 'some bash script', will write to the xterm window.
For your example, you could capture the error messages in the pipe (from popen directly by redirecting the standard error in the command to xterm's standard output, e.g.,
popen("xterm -display :0.0 -e ' some bash script ' 2>&1 ","r");
Capturing the output of the bash script is harder. You could redirect the output of the bash script itself, e.g.,
popen("xterm -display :0.0 -e ' some bash script >mylogfile ' ","r");
but that interferes with interaction. A better solution might be to use the script program, doing something like this:
popen("xterm -display :0.0 -e script mylogfile ' some bash script ' ","r");

How do I open a terminal window with C++ in Ubuntu?

I recently decided to start teaching myself C++ and thought a simple encryption project would be a good place to start, since it covers most of the basics (cout, cin, opening files, etc). Is there a way to have the code open a terminal window similar to the one opened when I compile and run from sublime text?
I have tried this so far, but it hasn't changed anything.
string cmd = "gnome-terminal-x sh-c 'ls-l; exec bash'";
system(cmd.c_str());
Essentially, I would like to be able to run the program by clicking the .exe, and have the terminal where all of the input and output goes pop up.
You don't need to write any code, you just need to configure the shortcut to launch the program in a terminal. Here's a Gnome dialog that shows that option:
Problem seems to be gnome-terminal, or then just my failure to give it the right arguments. For example gnome-terminal -x sh -c 'ls -l ; exec bash' from command line in another terminal just opens an empty gnome-terminal and spits out a bunch of glib warnings to original terminal... (Note to readers: if you can give the right command that works for gnome-terminal, please let me know in comments or just edit this paragraph.)
However, using xterm works, for example xterm -e sh -c 'ls -l; exec bash', or a line for your code:
string cmd = "xterm -e sh -c 'ls -l; exec bash'";
As a side note, the command to open the default x terminal window of the DE is x-terminal-emulator, but it quite often has the practical problem of different terminals taking different arguments, so sadly you're probably better of using a specific terminal, like that xterm, and requiring that to be installed, or letting user to configure what terminal to use, with what arguments (though letting user to specify any command to be run can also be a security risk, if user is not always trusted).
Just be very careful with escaping. For example, when you test the command form command line, and then copy-paste it to C++ string literal, you need to escape every " and \ one more time for C++. If you have trouble with this, check out C++11 raw strings.
Escaping becomes extra important if you construct the command string at runtime, and especially if you accept user input and add that to the string. In that case, better search for and use some existing library like GLib, or sanitize the user input very carefully (ie. just paranoidically reject anything with chars, which may have a special meaning in shell in some context).
If you are actually asking, how can my program open a console window for itself similar to how Windows console programs behave, and redirect it's own stdin, stdout and stderr there, as if it was launched from command line, that that is not very easy from the same binary, and it is not commonly done like that in Unix.
If you want a behaviour like that, you could create a desktop shortcut, but more general way is to write a wrapper shell script, which starts your binary in a terminal. What kind of script exactly, depends on how you want it to behave exactly: what will it do with stdio, will it return or wait for program to exit, how do you want it to find the binary, how does it behave when run from command line instead of double-clicking from GUI, etc.

How to dump the entire GDB session to a file, including commands I type and their output?

In bash, I can use the script command, which dumps everything that shows on the shell to a file, including:
commands typed
PS1 line
stdout and stderr of commands
What is the equivalent in gdb?
I tried to run shell script from inside GDB, but after I hit return, I was in the shell and lost the shell prompt and could not run command any more. Moreover I could not use ctrl+c or ctrl+\ to exit. I needed to force kill the /bin/login tty2 to exit.
If you want to log GDB's output, you can use the GDB logging output commands, eg.
set logging file mylog.txt
set logging on
If you want to redirect your program's output to a file, you can use a redirect, eg.
run myprog > mylog.txt
see the chapter on program IO in the GDB manual for more information
Create a text file, i.e. gdbCommands.txt, with the following commands
set logging on my_log_file\nbt 10\nq
bt 10, indicates the number of lines (function calls) we need from the backtrace, in our example is 10 lines.
Execute gdb using the following command, assuming a core dump file core.2345
gdb -x gdbCommands.txt myApp core.2345
Open my_log_file and inspect backtrace!
howto-redirect-gdb-backtrace-output-to-a-text-file
I have logging enabled using:
set trace-commands on
set pagination off
set logging file $log
and show logging reports (to both to terminal and file):
+show logging
Currently logging to mylog.
Logs will be appended to the log file.
Output will be logged and displayed
If I print the value of a variable that also gets logged (to both to terminal and file):
+p myvar
$2 = 0
But if I do command like where or “info b” all I get logged to the file is:
+where
+info b
Anyone know why or how to fix it?
Have a look at the GDB documentation. Search for "Canned Sequences of Commands". There is a way to save GDB commands in a file and run them with the source command and you can use some GDB commands in these scripts to print information available to GDB (like echo, output and printf).
If you want that output in a file, use set logging file FILE.